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>
impl<const N: usize> DodecetArray<N>
Sourcepub fn new() -> Self
pub fn new() -> Self
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()));Sourcepub fn from_slice(values: &[u16]) -> Self
pub fn from_slice(values: &[u16]) -> Self
Sourcepub fn from_dodecets(data: [Dodecet; N]) -> Self
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),
]);Sourcepub fn as_inner_mut(&mut self) -> &mut [Dodecet; N]
pub fn as_inner_mut(&mut self) -> &mut [Dodecet; N]
Get mutable inner array
Sourcepub fn to_hex_string(&self) -> String
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");Sourcepub fn from_hex_str(s: &str) -> Result<Self>
pub fn from_hex_str(s: &str) -> Result<Self>
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);Sourcepub fn map<F>(self, f: F) -> Self
pub fn map<F>(self, f: F) -> Self
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);Sourcepub fn zip_map<F>(self, other: Self, f: F) -> Self
pub fn zip_map<F>(self, other: Self, f: F) -> Self
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);Methods from Deref<Target = [Dodecet; N]>§
1.57.0 · Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Returns a slice containing the entire array. Equivalent to &s[..].
1.57.0 · Sourcepub fn as_mut_slice(&mut self) -> &mut [T]
pub fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice containing the entire array. Equivalent to
&mut s[..].
1.77.0 · Sourcepub fn each_ref(&self) -> [&T; N]
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 · Sourcepub fn each_mut(&mut self) -> [&mut T; N]
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]);Sourcepub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
🔬This is a nightly-only experimental API. (split_array)
pub fn split_array_ref<const M: usize>(&self) -> (&[T; M], &[T])
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, &[]);
}Sourcepub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
🔬This is a nightly-only experimental API. (split_array)
pub fn split_array_mut<const M: usize>(&mut self) -> (&mut [T; M], &mut [T])
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]);Sourcepub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
🔬This is a nightly-only experimental API. (split_array)
pub fn rsplit_array_ref<const M: usize>(&self) -> (&[T], &[T; M])
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]);
}Sourcepub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
🔬This is a nightly-only experimental API. (split_array)
pub fn rsplit_array_mut<const M: usize>(&mut self) -> (&mut [T], &mut [T; M])
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>
impl<const N: usize> Clone for DodecetArray<N>
Source§fn clone(&self) -> DodecetArray<N>
fn clone(&self) -> DodecetArray<N>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const N: usize> Debug for DodecetArray<N>
impl<const N: usize> Debug for DodecetArray<N>
Source§impl<const N: usize> Default for DodecetArray<N>
impl<const N: usize> Default for DodecetArray<N>
Source§impl<const N: usize> Deref for DodecetArray<N>
impl<const N: usize> Deref for DodecetArray<N>
Source§impl<const N: usize> DerefMut for DodecetArray<N>
impl<const N: usize> DerefMut for DodecetArray<N>
Source§impl<const N: usize> Display for DodecetArray<N>
impl<const N: usize> Display for DodecetArray<N>
Source§impl<const N: usize> PartialEq for DodecetArray<N>
impl<const N: usize> PartialEq for DodecetArray<N>
Source§fn eq(&self, other: &DodecetArray<N>) -> bool
fn eq(&self, other: &DodecetArray<N>) -> bool
self and other values to be equal, and is used by ==.