Struct pin_array::PinArray

source ·
pub struct PinArray<T, const SIZE: usize> { /* private fields */ }
Expand description

A structurally pinned array of values

Implementations§

source§

impl<T, const SIZE: usize> PinArray<T, SIZE>

source

pub fn new(elements: [T; SIZE]) -> Self

Create a new PinArray from elements

source

pub const fn len(&self) -> usize

Get the length of the PinArray

assert_eq!(PinArray::new(['a', 'b', 'c']).len(), 3);
source

pub const fn is_empty(&self) -> bool

Check if the array is empty

assert!(PinArray::<u32, 0>::new([]).is_empty());
source

pub fn get(&self, idx: usize) -> Option<&T>

Attempt to get a reference to an element by index

Note this does not require Pin as a reference is trivially Unpin

let p = pin!(PinArray::new([1, 2, 3]));
assert_eq!(p.get(0), Some(&1));
assert_eq!(p.get(1), Some(&2));
assert_eq!(p.get(2), Some(&3));
source

pub fn get_pin(self: Pin<&mut Self>, idx: usize) -> Option<Pin<&mut T>>

Attempt to get a pinned reference to an element by index

Note this requires self to be pinned

let mut p = pin!(PinArray::new([1, 2, 3]));
assert_eq!(p.as_mut().get_pin(0), Some(Pin::new(&mut 1)));
assert_eq!(p.as_mut().get_pin(1), Some(Pin::new(&mut 2)));
assert_eq!(p.as_mut().get_pin(2), Some(Pin::new(&mut 3)));
source

pub fn as_ref_array(&self) -> [&T; SIZE]

Convert this PinArray to an array of references

Immutable counterpart to PinArray::as_pin_array

let p = pin!(PinArray::new(["a", "b"]));
assert_eq!(p.as_ref_array(), [&"a", &"b"]);
source

pub fn as_pin_array<'me>(self: Pin<&'me mut Self>) -> [Pin<&'me mut T>; SIZE]

Convert this pinned PinArray to an array of pinned mutable references

Mutable counterpart to PinArray::as_ref_array

let mut p = pin!(PinArray::new(["a", "b"]));
assert_eq!(p.as_pin_array(), [Pin::new(&mut "a"), Pin::new(&mut "b")]);
source

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

Get an iterator over references to the elements

let p = pin!(PinArray::new(['h', 'i']));
let mut i = p.iter();
assert_eq!(i.next(), Some(&'h'));
assert_eq!(i.next(), Some(&'i'));
assert_eq!(i.next(), None);
source

pub fn iter_mut(self: Pin<&mut Self>) -> IterMut<'_, T, SIZE>

Get an iterator over pinned mutable references to the elements

let mut p = pin!(PinArray::new(['h', 'i']));
let mut i = p.iter_mut();
assert_eq!(i.next(), Some(Pin::new(&mut 'h')));
assert_eq!(i.next(), Some(Pin::new(&mut 'i')));
assert_eq!(i.next(), None);

Trait Implementations§

source§

impl<T: Clone, const SIZE: usize> Clone for PinArray<T, SIZE>

source§

fn clone(&self) -> PinArray<T, SIZE>

Returns a copy 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 SIZE: usize> Debug for PinArray<T, SIZE>

source§

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

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

impl<T: Default, const SIZE: usize> Default for PinArray<T, SIZE>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T: Hash, const SIZE: usize> Hash for PinArray<T, SIZE>

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<T: Ord, const SIZE: usize> Ord for PinArray<T, SIZE>

source§

fn cmp(&self, other: &PinArray<T, SIZE>) -> 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 + PartialOrd,

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

impl<T: PartialEq, const SIZE: usize> PartialEq for PinArray<T, SIZE>

source§

fn eq(&self, other: &PinArray<T, SIZE>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd, const SIZE: usize> PartialOrd for PinArray<T, SIZE>

source§

fn partial_cmp(&self, other: &PinArray<T, SIZE>) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Copy, const SIZE: usize> Copy for PinArray<T, SIZE>

source§

impl<T: Eq, const SIZE: usize> Eq for PinArray<T, SIZE>

source§

impl<T, const SIZE: usize> StructuralPartialEq for PinArray<T, SIZE>

source§

impl<T: Unpin, const SIZE: usize> Unpin for PinArray<T, SIZE>

Auto Trait Implementations§

§

impl<T, const SIZE: usize> RefUnwindSafe for PinArray<T, SIZE>
where T: RefUnwindSafe,

§

impl<T, const SIZE: usize> Send for PinArray<T, SIZE>
where T: Send,

§

impl<T, const SIZE: usize> Sync for PinArray<T, SIZE>
where T: Sync,

§

impl<T, const SIZE: usize> UnwindSafe for PinArray<T, SIZE>
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> 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,

§

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>,

§

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>,

§

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.