use super::super::math::pre::*; pub trait FasterIndex { fn at(&self, idx: I) -> &T where usize: Cast; fn at_mut(&mut self, idx: I) -> &mut T where usize: Cast; } macro_rules! impl_faster_index { () => { fn at(&self, idx: I) -> &T where usize: Cast, { let i = usize(idx); #[cfg(debug_assertions)] { &self[i] } #[cfg(not(debug_assertions))] { unsafe { self.get_unchecked(i) } } } fn at_mut(&mut self, idx: I) -> &mut T where usize: Cast, { let i = usize(idx); #[cfg(debug_assertions)] { &mut self[i] } #[cfg(not(debug_assertions))] { unsafe { self.get_unchecked_mut(i) } } } }; } impl FasterIndex for Vec { impl_faster_index!(); } impl FasterIndex for [T; L] { impl_faster_index!(); } impl FasterIndex for [T] { impl_faster_index!(); } pub trait LastIdx { fn last_idx(&self) -> usize; } impl LastIdx for &str { fn last_idx(&self) -> usize { self.len().max(1) - 1 } } impl LastIdx for &[T] { fn last_idx(&self) -> usize { self.len().max(1) - 1 } } impl LastIdx for [T] { fn last_idx(&self) -> usize { (&self).last_idx() } } impl LastIdx for Vec { fn last_idx(&self) -> usize { self.as_slice().last_idx() } }