Struct Clamp

pub struct Clamp<Idx>(pub Idx);

Clamps an index, guaranteeing that it will only access valid elements of the slice.

Examples

#![feature(sliceindex_wrappers)]

use core::index::Clamp;

let s: &[usize] = &[0, 1, 2, 3];

assert_eq!(&3, &s[Clamp(6)]);
assert_eq!(&[1, 2, 3], &s[Clamp(1..6)]);
assert_eq!(&[] as &[usize], &s[Clamp(5..6)]);
assert_eq!(&[0, 1, 2, 3], &s[Clamp(..6)]);
assert_eq!(&[0, 1, 2, 3], &s[Clamp(..=6)]);
assert_eq!(&[] as &[usize], &s[Clamp(6..)]);

Fields

0: Idx

Trait Implementations

impl<Idx: Debug> Debug for Clamp<Idx>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl<T> SliceIndex<[T]> for Clamp<Range<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<Range<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeFrom<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeFrom<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeFull>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeInclusive<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeInclusive<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeTo<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeToInclusive<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<RangeToInclusive<usize>>

type Output = [T];
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

impl<T> SliceIndex<[T]> for Clamp<usize>

type Output = T;
fn get(self, slice: &[T]) -> Option<&Self::Output>
fn get_mut(self, slice: &mut [T]) -> Option<&mut Self::Output>
unsafe fn get_unchecked(self, slice: *const [T]) -> *const Self::Output
unsafe fn get_unchecked_mut(self, slice: *mut [T]) -> *mut Self::Output
fn index(self, slice: &[T]) -> &Self::Output
fn index_mut(self, slice: &mut [T]) -> &mut Self::Output

Auto Trait Implementations

impl<Idx> Freeze for Clamp<Idx> where Idx: Freeze,

impl<Idx> RefUnwindSafe for Clamp<Idx> where Idx: RefUnwindSafe,

impl<Idx> Send for Clamp<Idx> where Idx: Send,

impl<Idx> Sync for Clamp<Idx> where Idx: Sync,

impl<Idx> Unpin for Clamp<Idx> where Idx: Unpin,

impl<Idx> UnsafeUnpin for Clamp<Idx> where Idx: UnsafeUnpin,

impl<Idx> UnwindSafe for Clamp<Idx> where Idx: UnwindSafe,

Blanket Implementations

impl<T> Any for Clamp<Idx> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Clamp<Idx> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Clamp<Idx> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for Clamp<Idx>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Clamp<Idx> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Clamp<Idx>

impl<T, U> Into<U> for Clamp<Idx> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Clamp<Idx> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Clamp<Idx> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>