Trait index_ext::Int [−][src]
pub trait Int: Sealed {
fn get_int<T>(&self, idx: T) -> Option<&<T as IntSliceIndex<Self>>::Output>
where
T: IntSliceIndex<Self>;
fn get_int_mut<T>(
&mut self,
idx: T
) -> Option<&mut <T as IntSliceIndex<Self>>::Output>
where
T: IntSliceIndex<Self>;
unsafe fn get_int_unchecked<T>(
&self,
idx: T
) -> &<T as IntSliceIndex<Self>>::Output
where
T: IntSliceIndex<Self>;
unsafe fn get_int_unchecked_mut<T>(
&mut self,
idx: T
) -> &mut <T as IntSliceIndex<Self>>::Output
where
T: IntSliceIndex<Self>;
}Expand description
An extension trait allowing slices to be indexed by everything convertible to usize.
Required methods
fn get_int<T>(&self, idx: T) -> Option<&<T as IntSliceIndex<Self>>::Output> where
T: IntSliceIndex<Self>,
fn get_int<T>(&self, idx: T) -> Option<&<T as IntSliceIndex<Self>>::Output> where
T: IntSliceIndex<Self>, Return a reference to an element or subslice with an integer index, or None if out of
bounds.
This works like slice::get but allows arbitrary integers to be used as indices. It will
first try to convert them to an usize. For some types (u8 and u16) this can never
fail while other types may refer to negative indices or are out-of-range. These cases are
treated as if the index was out-of-bounds due to the slice being too short.
Examples
let v = [10, 40, 30]; assert_eq!(Some(&40), v.get_int(1u64)); assert_eq!(Some(&[10, 40][..]), v.get_int(0u8..2)); assert_eq!(None, v.get_int(3u8)); assert_eq!(None, v.get_int(0u8..4)); assert_eq!(None, v.get_int(-1i8));
fn get_int_mut<T>(
&mut self,
idx: T
) -> Option<&mut <T as IntSliceIndex<Self>>::Output> where
T: IntSliceIndex<Self>,
fn get_int_mut<T>(
&mut self,
idx: T
) -> Option<&mut <T as IntSliceIndex<Self>>::Output> where
T: IntSliceIndex<Self>, Return a mutable reference to an element or subslice with an integer index, or None if
out of bounds.
This works like slice::get_mut.
Examples
let x = &mut [0, 1, 2]; if let Some(elem) = x.get_int_mut(1u8) { *elem = 42; } assert_eq!(x, &[0, 42, 2]);
unsafe fn get_int_unchecked<T>(
&self,
idx: T
) -> &<T as IntSliceIndex<Self>>::Output where
T: IntSliceIndex<Self>,
unsafe fn get_int_unchecked<T>(
&self,
idx: T
) -> &<T as IntSliceIndex<Self>>::Output where
T: IntSliceIndex<Self>, Returns a reference to an element or subslice without doing bounds checking.
Safety
Like slice::get_unchecked, calling this method with an out of bounds index is undefined
behaviour. This includes indices for which conversion to a usize fails.
Examples
let x = &[1, 2, 4]; unsafe { assert_eq!(x.get_int_unchecked(1i8), &2); }
unsafe fn get_int_unchecked_mut<T>(
&mut self,
idx: T
) -> &mut <T as IntSliceIndex<Self>>::Output where
T: IntSliceIndex<Self>,
unsafe fn get_int_unchecked_mut<T>(
&mut self,
idx: T
) -> &mut <T as IntSliceIndex<Self>>::Output where
T: IntSliceIndex<Self>, Returns a mutable reference to an element or subslice without doing bounds checking.
Safety
Like slice::get_unchecked_mut, calling this method with an out of bounds index is undefined
behaviour. This includes indices for which conversion to a usize fails.
Examples
let x = &mut [1, 2, 4]; unsafe { let elem = x.get_int_unchecked_mut(1u64); *elem = 13; } assert_eq!(x, &[1, 13, 4]);