Associate types for function arguments with a lifetime (i.e., ArrayViews) #1269
Unanswered
StefanUlbrich
asked this question in
Q&A
Replies: 1 comment
-
I'm not certain how helpful this is for you, but https://lab.whitequark.org/notes/2016-12-13/abstracting-over-mutability-in-rust/ describes some general patterns for creating traits and structs that are generic over the mutability of references. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I struggle for some time with lifetimes and associated times when it comes to handling
ArrayView
function arguments. Essentially, I want to make the following method more general by accepting shared or mutable references to arrays (okay, this might not make that much sense) or even support tensors on the GPU from different frameworks:Looking for lifetime-related problems with associated types, I only could find solutions for when the type's lifetime is tied to the object's lifetime (e.g., when returning a reference to a struct member). This surprised me as I think this use-case (function arguments with a lifetime) cannot be that rare (at least not with
ndarray
and the concept of views).After a long try-and-error phase (and waiting for generic associated types GAT), I came up with this code that at least compiles (but I think it's correct).
My question is whether someone could shed some light on what is happening here, if it's what I expect and if I could have done it without GAT. I understand that I basically say "the
data
type could have a lifetime parameter and the compiler should figure out the lifetime fordo_something
on its own". Why can't I omit the<'_>
like in theOriginal
trait (at least, the compiler hints me at doing the right thing).Any help and enlightenment would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions