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>
impl<const N: usize, T> ArraySection<T, N>
Sourcepub const fn new(array: [T; N], section: Range<usize>) -> Self
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.
Sourcepub const fn start(&self) -> usize
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
.
Sourcepub const fn end(&self) -> usize
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
.
Sourcepub fn change_section(&mut self, section: Range<usize>)
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.
Sourcepub const fn try_as_full_array(&self) -> Option<&[T; N]>
pub const fn try_as_full_array(&self) -> Option<&[T; N]>
Returns a reference to the full underlying array if it is fully populated.
Sourcepub fn try_as_full_array_mut(&mut self) -> Option<&mut [T; N]>
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.
Sourcepub const fn as_full_array(&self) -> &[T; N]
pub const fn as_full_array(&self) -> &[T; N]
Returns a reference to the full underlying array.
Sourcepub fn as_full_array_mut(&mut self) -> &mut [T; N]
pub fn as_full_array_mut(&mut self) -> &mut [T; N]
Returns a mutable reference to the full underlying array.
Sourcepub const fn split_at_section(&self) -> (&[T], &[T], &[T])
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.
Sourcepub fn split_at_section_mut(&mut self) -> (&mut [T], &mut [T], &mut [T])
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.
Sourcepub fn into_full_array(self) -> [T; N]
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
Sourcepub fn as_slice_mut(&mut self) -> &mut [T]
pub fn as_slice_mut(&mut self) -> &mut [T]
Returns the section of the array as a mutable slice.
Sourcepub const fn section_is_full_array(&self) -> bool
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
.
Sourcepub fn iter(&self) -> ArraySectionIter<'_, T> ⓘ
pub fn iter(&self) -> ArraySectionIter<'_, T> ⓘ
Returns an iterator over the array section.
Sourcepub fn iter_mut(&mut self) -> ArraySectionIterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> ArraySectionIterMut<'_, T> ⓘ
Returns a mutable iterator over the array section.
Sourcepub fn into_vec(self) -> Vec<T>
Available on crate features alloc
or std
only.
pub fn into_vec(self) -> Vec<T>
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]);
Sourcepub fn into_boxed_slice(self) -> Box<[T]>
Available on crate features alloc
or std
only.
pub fn into_boxed_slice(self) -> Box<[T]>
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>
impl<T: Clone, const N: usize> ArraySection<T, N>
Source§impl<T: Copy, const N: usize> ArraySection<T, N>
impl<T: Copy, const N: usize> ArraySection<T, N>
Sourcepub const fn into_full_array_const(self) -> [T; N]
pub const fn into_full_array_const(self) -> [T; N]
Converts self
into the full underlying array.
Trait Implementations§
Source§impl<T: Clone, const N: usize> Clone for ArraySection<T, N>
impl<T: Clone, const N: usize> Clone for ArraySection<T, N>
Source§fn clone(&self) -> ArraySection<T, N>
fn clone(&self) -> ArraySection<T, N>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: Clone, const N: usize> From<ArraySection<T, N>> for Box<[T]>
Available on crate features alloc
or std
only.
impl<T: Clone, const N: usize> From<ArraySection<T, N>> for Box<[T]>
alloc
or std
only.Source§impl<T: Clone, const N: usize> From<ArraySection<T, N>> for Vec<T>
Available on crate features alloc
or std
only.
impl<T: Clone, const N: usize> From<ArraySection<T, N>> for Vec<T>
alloc
or std
only.Source§impl<T, const N: usize> From<TryFromArraySectionError<T, N>> for ArraySection<T, N>
impl<T, const N: usize> From<TryFromArraySectionError<T, N>> for ArraySection<T, N>
Source§fn from(value: TryFromArraySectionError<T, N>) -> Self
fn from(value: TryFromArraySectionError<T, N>) -> Self
Source§impl<const N: usize, T: Hash> Hash for ArraySection<T, N>
Only hashes the data in the section, and not the full array.
impl<const N: usize, T: Hash> Hash for ArraySection<T, N>
Only hashes the data in the section, and not the full array.
Source§impl<const N: usize, T, I: SliceIndex<[T]>> Index<I> for ArraySection<T, N>
impl<const N: usize, T, I: SliceIndex<[T]>> Index<I> for ArraySection<T, N>
Source§impl<'a, const N: usize, T> IntoIterator for &'a ArraySection<T, N>
impl<'a, const N: usize, T> IntoIterator for &'a ArraySection<T, N>
Source§impl<'a, const N: usize, T> IntoIterator for &'a mut ArraySection<T, N>
impl<'a, const N: usize, T> IntoIterator for &'a mut ArraySection<T, N>
Source§impl<const N: usize, T> IntoIterator for ArraySection<T, N>
impl<const N: usize, T> IntoIterator for ArraySection<T, N>
Source§impl<const N: usize, T: Ord> Ord for ArraySection<T, N>
Only compares the data in the sections and not the full arrays.
impl<const N: usize, T: Ord> Ord for ArraySection<T, N>
Only compares the data in the sections and not the full arrays.
Source§impl<const N: usize, const M: usize, T, U> PartialEq<ArraySection<T, N>> for ArraySection<U, M>
impl<const N: usize, const M: usize, T, U> PartialEq<ArraySection<T, N>> for ArraySection<U, M>
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.
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§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.
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.