Struct ArraySection

Source
pub struct ArraySection<T, const N: usize> { /* private fields */ }
Expand description

An array where only a section of the data may be viewed, as the other data may e.g. not uphold some invariant.

Indexing into the ArraySection indexes only into the section:

//                                                     v  v
const AS: ArraySection<i32, 4> = ArraySection::new([0, 1, 2, 0], 1..3);
assert_eq![AS[0], 1];
assert_eq![AS[1], 2];

The other data is not considered in comparisons, ordering or hashing:

//                       v  v
const A1: [i32; 4] = [1, 3, 7, 1];
const A2: [i32; 5] = [0, 3, 7, 100, -5];
const AS1: ArraySection<i32, 4> = ArraySection::new(A1, 1..3);
const AS2: ArraySection<i32, 5> = ArraySection::new(A2, 1..3);

// Even though the arrays are different
assert_ne!(A1.as_slice(), A2.as_slice());
// The sections are the same
assert_eq!(AS1, AS2);

Implementations§

Source§

impl<const N: usize, T> ArraySection<T, N>

Source

pub const fn new(array: [T; N], section: Range<usize>) -> Self

Restrict an array so that only elements within the given range are visible.

§Panics

Panics if the range of indices is out of bounds of the array.

Source

pub const fn start(&self) -> usize

Returns the first index of the full underlying array that is part of the section. I.e. the section is the subrange start ..end.

Source

pub const fn end(&self) -> usize

Returns the first index of the full underlying array that is outside the section (to the right). I.e. the section is the subrange start.. end.

Source

pub fn change_section(&mut self, section: Range<usize>)

Changes the section of the array to the given one.

§Panics

Panics if the start and/or end of the given section is out of bounds of the array.

Source

pub const fn try_as_full_array(&self) -> Option<&[T; N]>

Returns a reference to the full underlying array if it is fully populated.

Source

pub fn try_as_full_array_mut(&mut self) -> Option<&mut [T; N]>

Returns a mutable reference to the full underlying array if it is fully populated.

Source

pub const fn as_full_array(&self) -> &[T; N]

Returns a reference to the full underlying array.

Source

pub fn as_full_array_mut(&mut self) -> &mut [T; N]

Returns a mutable reference to the full underlying array.

Source

pub const fn split_at_section(&self) -> (&[T], &[T], &[T])

Splits the underlying array into three slices: the part before the section, the section, and the part after the section.

Source

pub fn split_at_section_mut(&mut self) -> (&mut [T], &mut [T], &mut [T])

Splits the underlying array into three mutable slices: the part before the section, the section, and the part after the seciton.

Source

pub fn into_full_array(self) -> [T; N]

Converts self into the full underlying array.

If you wish to use this in const context the destructor of T must be trivial, use into_full_array_const

Source

pub const fn as_slice(&self) -> &[T]

Returns the section of the array as a slice.

Source

pub fn as_slice_mut(&mut self) -> &mut [T]

Returns the section of the array as a mutable slice.

Source

pub const fn len(&self) -> usize

Returns the length of the array section.

Source

pub const fn is_empty(&self) -> bool

Returns whether the array section is empty.

Source

pub const fn section_is_full_array(&self) -> bool

Returns whether the section is just the entire array. If this is true it is completely fine to call as_full_array or into_full_array.

Source

pub fn iter(&self) -> ArraySectionIter<'_, T>

Returns an iterator over the array section.

Source

pub fn iter_mut(&mut self) -> ArraySectionIterMut<'_, T>

Returns a mutable iterator over the array section.

Source

pub fn into_vec(self) -> Vec<T>

Available on crate features alloc or std only.

Converts the array section into a vector.

§Example
//                                     v  v  v
let section = ArraySection::new([0, 0, 1, 2, 3, 0, 0], 2..5);

assert_eq!(section.into_vec(), vec![1, 2, 3]);
Source

pub fn into_boxed_slice(self) -> Box<[T]>

Available on crate features alloc or std only.

Converts the array section into a boxed slice.

§Example
//                                     v  v  v
let section = ArraySection::new([0, 0, 1, 2, 3, 0, 0], 2..5);

assert_eq!(
    section.into_boxed_slice(),
    Box::new([1, 2, 3]) as Box<[i32]>
);
Source§

impl<T: Clone, const N: usize> ArraySection<T, N>

Source

pub fn to_vec(&self) -> Vec<T>

Available on crate features alloc or std only.

Clones the contents of the array section into a vector.

Source

pub fn to_boxed_slice(&self) -> Box<[T]>

Available on crate features alloc or std only.

Clones the contents of the array section into a boxed slice.

Source§

impl<T: Copy, const N: usize> ArraySection<T, N>

Source

pub const fn into_full_array_const(self) -> [T; N]

Converts self into the full underlying array.

Trait Implementations§

Source§

impl<const N: usize, T> AsRef<[T]> for ArraySection<T, N>

Source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone, const N: usize> Clone for ArraySection<T, N>

Source§

fn clone(&self) -> ArraySection<T, N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, const N: usize> Debug for ArraySection<T, N>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<const N: usize, T> From<[T; N]> for ArraySection<T, N>

Source§

fn from(value: [T; N]) -> Self

Converts to this type from the input type.
Source§

impl<T: Clone, const N: usize> From<ArraySection<T, N>> for Box<[T]>

Available on crate features alloc or std only.
Source§

fn from(value: ArraySection<T, N>) -> Box<[T]>

Clones the contents of the section into a Boxed slice.

Source§

impl<T: Clone, const N: usize> From<ArraySection<T, N>> for Vec<T>

Available on crate features alloc or std only.
Source§

fn from(value: ArraySection<T, N>) -> Vec<T>

Clones the contents of the section into a Vec.

Source§

impl<T, const N: usize> From<TryFromArraySectionError<T, N>> for ArraySection<T, N>

Source§

fn from(value: TryFromArraySectionError<T, N>) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize, T: Hash> Hash for ArraySection<T, N>

Only hashes the data in the section, and not the full array.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: usize, T, I: SliceIndex<[T]>> Index<I> for ArraySection<T, N>

Source§

type Output = <I as SliceIndex<[T]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, const N: usize, T> IntoIterator for &'a ArraySection<T, N>

Source§

type IntoIter = ArraySectionIter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = <ArraySectionIter<'a, T> as Iterator>::Item

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, const N: usize, T> IntoIterator for &'a mut ArraySection<T, N>

Source§

type IntoIter = ArraySectionIter<'a, T>

Which kind of iterator are we turning this into?
Source§

type Item = <ArraySectionIter<'a, T> as Iterator>::Item

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<const N: usize, T> IntoIterator for ArraySection<T, N>

Source§

type IntoIter = ArraySectionIntoIter<T, N>

Which kind of iterator are we turning this into?
Source§

type Item = <ArraySectionIntoIter<T, N> as Iterator>::Item

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<const N: usize, T: Ord> Ord for ArraySection<T, N>

Only compares the data in the sections and not the full arrays.

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: usize, const M: usize, T, U> PartialEq<[T; N]> for ArraySection<U, M>
where [U]: PartialEq<[T]>,

Source§

fn eq(&self, other: &[T; N]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, T, U> PartialEq<[U]> for ArraySection<T, N>
where U: PartialEq<T>,

Source§

fn eq(&self, other: &[U]) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, T, U> PartialEq<ArraySection<T, N>> for [U]
where U: PartialEq<T>,

Source§

fn eq(&self, other: &ArraySection<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, const M: usize, T, U> PartialEq<ArraySection<T, N>> for ArraySection<U, M>
where [U]: PartialEq<[T]>,

Source§

fn eq(&self, other: &ArraySection<T, N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, const M: usize, T, U> PartialEq<ArraySection<U, M>> for [T; N]
where [T]: PartialEq<[U]>,

Source§

fn eq(&self, other: &ArraySection<U, M>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: usize, const M: usize, T: PartialOrd> PartialOrd<ArraySection<T, M>> for ArraySection<T, N>

Only checks the data in the sections, and not the full arrays.

Source§

fn partial_cmp(&self, other: &ArraySection<T, M>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: usize, T> TryFrom<ArraySection<T, N>> for [T; N]

Converts the ArraySection into an array if the section is actually the entire array.

Source§

type Error = TryFromArraySectionError<T, N>

The type returned in the event of a conversion error.
Source§

fn try_from(value: ArraySection<T, N>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Copy, const N: usize> Copy for ArraySection<T, N>

Source§

impl<T: Eq, const N: usize> Eq for ArraySection<T, N>

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for ArraySection<T, N>
where T: Freeze,

§

impl<T, const N: usize> RefUnwindSafe for ArraySection<T, N>
where T: RefUnwindSafe,

§

impl<T, const N: usize> Send for ArraySection<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for ArraySection<T, N>
where T: Sync,

§

impl<T, const N: usize> Unpin for ArraySection<T, N>
where T: Unpin,

§

impl<T, const N: usize> UnwindSafe for ArraySection<T, N>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.