Skip to main content

DodecetArray

Struct DodecetArray 

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

A fixed-size array of dodecets

§Example

use dodecet_encoder::{Dodecet, DodecetArray};

let arr = DodecetArray::<3>::from_slice(&[0x123, 0x456, 0x789]);
assert_eq!(arr[0], Dodecet::from_hex(0x123));

Implementations§

Source§

impl<const N: usize> DodecetArray<N>

Source

pub fn new() -> Self
where [Dodecet; N]: Sized,

Create a new dodecet array initialized to zeros

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr: DodecetArray<3> = DodecetArray::new();
assert!(arr.iter().all(|d| d.is_zero()));
Source

pub fn from_slice(values: &[u16]) -> Self
where [Dodecet; N]: Sized,

Create from a slice of u16 values

§Panics

Panics if slice length != N or any value > 4095

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr = DodecetArray::<3>::from_slice(&[0x123, 0x456, 0x789]);
Source

pub fn from_dodecets(data: [Dodecet; N]) -> Self

Create from dodecets

§Example
use dodecet_encoder::{Dodecet, DodecetArray};

let arr = DodecetArray::<3>::from_dodecets([
    Dodecet::from_hex(0x123),
    Dodecet::from_hex(0x456),
    Dodecet::from_hex(0x789),
]);
Source

pub fn as_inner(&self) -> &[Dodecet; N]

Get inner array

Source

pub fn as_inner_mut(&mut self) -> &mut [Dodecet; N]

Get mutable inner array

Source

pub fn to_hex_string(&self) -> String

Convert to hex string (concatenated)

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr = DodecetArray::<2>::from_slice(&[0x123, 0x456]);
assert_eq!(arr.to_hex_string(), "123456");
Source

pub fn from_hex_str(s: &str) -> Result<Self>
where [Dodecet; N]: Sized,

Parse from hex string

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr = DodecetArray::<2>::from_hex_str("123456").unwrap();
assert_eq!(arr[0].value(), 0x123);
assert_eq!(arr[1].value(), 0x456);
Source

pub fn map<F>(self, f: F) -> Self
where F: FnMut(Dodecet) -> Dodecet,

Map each dodecet through a function

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr = DodecetArray::<3>::from_slice(&[0x100, 0x200, 0x300]);
let doubled = arr.map(|d| Dodecet::from_hex(d.value() * 2));
assert_eq!(doubled[0].value(), 0x200);
Source

pub fn zip_map<F>(self, other: Self, f: F) -> Self
where F: FnMut(Dodecet, Dodecet) -> Dodecet,

Zip with another array

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let a = DodecetArray::<3>::from_slice(&[0x100, 0x200, 0x300]);
let b = DodecetArray::<3>::from_slice(&[0x001, 0x002, 0x003]);
let sum = a.zip_map(b, |x, y| x + y);
assert_eq!(sum[0].value(), 0x101);
Source

pub fn sum(self) -> Dodecet

Sum all dodecets

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr = DodecetArray::<3>::from_slice(&[0x100, 0x200, 0x300]);
assert_eq!(arr.sum().value(), 0x600);
Source

pub fn average(&self) -> Dodecet

Get the average

§Example
use dodecet_encoder::{DodecetArray, Dodecet};

let arr = DodecetArray::<3>::from_slice(&[0x000, 0x003, 0x006]);
let avg = arr.average();
assert_eq!(avg.value(), 0x003);

Methods from Deref<Target = [Dodecet; N]>§

1.57.0 · Source

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

Returns a slice containing the entire array. Equivalent to &s[..].

1.57.0 · Source

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

Returns a mutable slice containing the entire array. Equivalent to &mut s[..].

1.77.0 · Source

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

Borrows each element and returns an array of references with the same size as self.

§Example
let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
1.77.0 · Source

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

Borrows each element mutably and returns an array of mutable references with the same size as self.

§Example

let mut floats = [3.1, 2.7, -1.0];
let float_refs: [&mut f64; 3] = floats.each_mut();
*float_refs[0] = 0.0;
assert_eq!(float_refs, [&mut 0.0, &mut 2.7, &mut -1.0]);
assert_eq!(floats, [0.0, 2.7, -1.0]);
Source

pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
Source

pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.split_array_mut::<2>();
assert_eq!(left, &mut [1, 0][..]);
assert_eq!(right, &mut [3, 0, 5, 6]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);
Source

pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}
Source

pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])

🔬This is a nightly-only experimental API. (split_array)

Divides one mutable array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

§Panics

Panics if M > N.

§Examples
#![feature(split_array)]

let mut v = [1, 0, 3, 0, 5, 6];
let (left, right) = v.rsplit_array_mut::<4>();
assert_eq!(left, &mut [1, 0]);
assert_eq!(right, &mut [3, 0, 5, 6][..]);
left[1] = 2;
right[1] = 4;
assert_eq!(v, [1, 2, 3, 4, 5, 6]);

Trait Implementations§

Source§

impl<const N: usize> Clone for DodecetArray<N>

Source§

fn clone(&self) -> DodecetArray<N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for DodecetArray<N>

Source§

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

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

impl<const N: usize> Default for DodecetArray<N>
where [Dodecet; N]: Sized,

Source§

fn default() -> Self

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

impl<const N: usize> Deref for DodecetArray<N>

Source§

type Target = [Dodecet; N]

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<const N: usize> DerefMut for DodecetArray<N>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<const N: usize> Display for DodecetArray<N>

Source§

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

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

impl<const N: usize> From<[Dodecet; N]> for DodecetArray<N>

Source§

fn from(data: [Dodecet; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> From<[u16; N]> for DodecetArray<N>

Source§

fn from(values: [u16; N]) -> Self

Converts to this type from the input type.
Source§

impl<const N: usize> PartialEq for DodecetArray<N>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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> Eq for DodecetArray<N>

Source§

impl<const N: usize> StructuralPartialEq for DodecetArray<N>

Auto Trait Implementations§

§

impl<const N: usize> Freeze for DodecetArray<N>

§

impl<const N: usize> RefUnwindSafe for DodecetArray<N>

§

impl<const N: usize> Send for DodecetArray<N>

§

impl<const N: usize> Sync for DodecetArray<N>

§

impl<const N: usize> Unpin for DodecetArray<N>

§

impl<const N: usize> UnsafeUnpin for DodecetArray<N>

§

impl<const N: usize> UnwindSafe for DodecetArray<N>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.