pub trait Index {
type Ref;
// Required method
fn get(&self, index: usize) -> Self::Ref;
// Provided methods
fn last(&self) -> Option<Self::Ref>
where Self: Len { ... }
fn index_iter(&self) -> IterOwn<&Self> ⓘ { ... }
fn into_index_iter(self) -> IterOwn<Self> ⓘ
where Self: Sized { ... }
}
Expand description
A type that can be accessed by usize
but without borrowing self
.
This can be useful for types which include their own lifetimes, and
which wish to express that their reference has the same lifetime.
In the GAT Index
, the Ref<'_>
lifetime would be tied to &self
.
This trait may be challenging to implement for owning containers,
for example Vec<_>
, which would need their Ref
type to depend
on the lifetime of the &self
borrow in the get()
function.
Required Associated Types§
Required Methods§
Provided Methods§
fn last(&self) -> Option<Self::Ref>where
Self: Len,
Sourcefn index_iter(&self) -> IterOwn<&Self> ⓘ
fn index_iter(&self) -> IterOwn<&Self> ⓘ
Converts &self
into an iterator.
This has an awkward name to avoid collision with iter()
, which may also be implemented.
Sourcefn into_index_iter(self) -> IterOwn<Self> ⓘwhere
Self: Sized,
fn into_index_iter(self) -> IterOwn<Self> ⓘwhere
Self: Sized,
Converts self
into an iterator.
This has an awkward name to avoid collision with into_iter()
, which may also be implemented.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.