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.
§Performance
A call to get(index) will attempt to access member slices at index,
and as these could panic the optimizer cannot eliminate them even if you
do not then go on to examine the values. If you plan to access a field
(for tuples or structs) or variant match (for enums) you should perform
this before calling get(index) when able.
Required Associated Types§
Required Methods§
Sourcefn get(&self, index: usize) -> Self::Ref
fn get(&self, index: usize) -> Self::Ref
Returns the reference type for location index.
Implementations should most likely be marked #[inline(always)].
If possible, avoid the potential to panic in these implementations,
as this prevents Rust/LLVM from eliding the test even if the return
value is not actually consumed.
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.