Struct array_section::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
indices 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 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 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 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 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.
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, 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>
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<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>
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<[T; N]> for ArraySection<U, M>
impl<const N: usize, const M: usize, T, U> PartialEq<[T; N]> for ArraySection<U, M>
source§impl<const N: usize, T, U> PartialEq<ArraySection<T, N>> for [U]where
U: PartialEq<T>,
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
fn eq(&self, other: &ArraySection<T, N>) -> bool
self
and other
values to be equal, and is used
by ==
.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§fn eq(&self, other: &ArraySection<T, N>) -> bool
fn eq(&self, other: &ArraySection<T, N>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<const N: usize, const M: usize, T, U> PartialEq<ArraySection<U, M>> for [T; N]
impl<const N: usize, const M: usize, T, U> PartialEq<ArraySection<U, M>> for [T; N]
source§fn eq(&self, other: &ArraySection<U, M>) -> bool
fn eq(&self, other: &ArraySection<U, M>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<const N: usize, const M: usize, T: PartialOrd> PartialOrd<ArraySection<T, M>> for ArraySection<T, N>
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>
fn partial_cmp(&self, other: &ArraySection<T, M>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<const N: usize, T> TryFrom<ArraySection<T, N>> for [T; N]
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.