ArrayOfBase

Struct ArrayOfBase 

Source
pub struct ArrayOfBase<const N: usize, const B: u8> { /* private fields */ }
Expand description

An array of specified length N of u8 values from 0 to B-1

§Example - Success

// GIVEN
let array = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4];

// WHEN
let actual = ArrayOfBase::<10, 5>::try_new(array);

// THEN
assert!(actual.is_ok())

§Example - Error

// GIVEN
let array = [16];

// WHEN
let actual = ArrayOfBase::<1, 16>::try_new(array);

// THEN
assert!(actual.is_err())

Implementations§

Source§

impl<const N: usize, const B: u8> ArrayOfBase<N, B>

Source

pub fn try_new(v: [u8; N]) -> Result<Self, Error>

Create from Array of u8 numbers of the same size

See ArrayOfBase for Example

§Errors

Will error if:

§Example
// GIVEN
let valid_array     = [0u8, 1, 2, 3, 0];
let invalid_array   = [0u8, 1, 2, 3, 4];

// WHEN
ArrayOfBase::<5, 4>::try_new(v).map(|b| b.unwrap())
// Ran on each

// THEN
assert_eq!(
    actual,
    vec![
        Ok([0u8, 1, 2, 3, 0]),      // valid_array
        Err(Error::InvalidValue),   // invalid_array
    ]
);
Source

pub fn try_from_vec_exact(v: &[u8]) -> Result<Self, Error>

Create from Vec of u8 numbers

§Errors

Will error if:

§Example
// GIVEN
let valid_vec: Vec<u8>      = vec![0, 0, 1, 2, 3];
let undersized_vec: Vec<u8> = vec![1];
let empty_vec: Vec<u8>      = Vec::new();
let oversized_vec: Vec<u8>  = vec![1, 2, 3, 1, 2, 3];

// WHEN
ArrayOfBase::<5, 4>::try_from_vec_exact(v).map(|b| b.unwrap())
// Ran on each

// THEN
assert_eq!(
    actual,
    vec![
        Ok([0u8, 0, 1, 2, 3]),  // valid_vec
        Err(Error::Undersized), // undersized_vec
        Err(Error::Empty),      // empty_vec
        Err(Error::Oversized),  // oversized_vec
    ]
);
Source

pub fn try_from_vec_pad(v: &[u8]) -> Result<Self, Error>

Create from non-empty Vec of u8 numbers, pad 0s to the left

§Errors

Will error if:

§Example
// GIVEN
let valid_vec: Vec<u8>      = vec![1, 2, 3];
let empty_vec: Vec<u8>      = Vec::new();
let oversized_vec: Vec<u8>  = vec![1, 2, 3, 1, 2, 3];

// WHEN
ArrayOfBase::<5, 4>::try_from_vec_pad(v).map(|b| b.unwrap())
// Ran on each

// THEN
assert_eq!(
    actual,
    vec![
        Ok([0u8, 0, 1, 2, 3]),  // valid_vec
        Err(Error::Empty),      // empty_vec
        Err(Error::Oversized),  // oversized_vec
    ]
);
Source

pub fn unwrap(self) -> [u8; N]

Return the contained verified array

§Example
// GIVEN
verified = ArrayOfBase::try_new(valid_u8_array).unwrap();

// WHEN
let actual = verified.unwrap();

// THEN
assert_eq!(actual, valid_u8_array)
Source

pub fn borrow_u8_array(&self) -> &[u8; N]

Borrow the contained verified array

§Example
// GIVEN
verified = ArrayOfBase::try_new(VALID_U8_ARRAY).unwrap();

// WHEN
let actual = verified.borrow_u8_array();

// THEN
assert_eq!(actual, &VALID_U8_ARRAY)
Source

pub fn pad_array(v: &[u8]) -> [u8; N]

Prepend to array 0 values until N size reached

§Panics

Will Panic if an array that exceeds the desired length is passed

§Example
// GIVEN
let x = [1, 2, 3];

// WHEN
let actual = ArrayOfBase::<5, 10>::pad_array(&x);

// THEN
assert_eq!(actual, [0u8, 0, 1, 2, 3]);
Source

pub fn trim<const L: usize>(&self) -> ArrayOfBase<L, B>

Reduce the size of the array to the specified value

§Panics

If the specified size L is larger than the current array size N the function will panic

§Example
// GIVEN
let long_array =
    ArrayOfBase::<5, 4>::try_new([0u8, 1, 2, 3, 0]).unwrap();

// WHEN
let actual: ArrayOfBase<3, 4> = long_array.trim();

// THEN
assert_eq!(actual.borrow_u8_array(), &[0u8, 1, 2]);
Source§

impl<const N: usize> ArrayOfBase<N, 16>

Source

pub fn from_u8_array<const I: usize>(array: [u8; I]) -> ArrayOfHex<N>

Split u8 values into u4 values, construct into ArrayOfHex

§Todo

TODO: Replace with from_u8_array(array: [u8; {N / 2}]) when generic_const_exprs feature stabilizes

§Panics
  • If the I value is not half N the function will panic.
  • If N is not an even number, the function will panic.

This will be resolved when generic_const_exprs feature stabilizes

§Example
// GIVEN
let u8_array = [0u8, 16, 255];

// WHEN
let actual = ArrayOfHex::<6>::from_u8_array(u8_array);
//                     ^^^^^ - Required until `generic_const_exprs` feature stabilizes

// THEN
assert_eq!(actual.borrow_u8_array(), &[0u8, 0, 1, 0, 15, 15]);

Trait Implementations§

Source§

impl<const N: usize, const B: u8> Clone for ArrayOfBase<N, B>

Source§

fn clone(&self) -> ArrayOfBase<N, B>

Returns a duplicate 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<const N: usize, const B: u8> Debug for ArrayOfBase<N, B>

Source§

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

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

impl<const N: usize, const B: u8> Default for ArrayOfBase<N, B>

Source§

fn default() -> Self

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

impl<const N: usize> Display for ArrayOfBase<N, 16>

Source§

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

Output the contents of ArrayOfHex as a hexadecimal string

§Restrictions

This function only works if:

§Example
// GIVEN
let value: ArrayOfHex<16> = ALL_HEX_VALUES();

// WHEN
let actual: String = value.to_string();

// THEN
assert_eq!(actual, "0123456789abcdef");
Source§

impl<const N: usize> FromStr for ArrayOfBase<N, 16>

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parse a hexadecimal string into ArrayOfHex

§Restrictions

This function only works if:

§Example
// GIVEN
let string = "0123456789AbCdEf";

// WHEN
let actual = string.parse::<ArrayOfHex<16>>();

// THEN
assert_eq!(
    actual.ok().unwrap().borrow_u8_array(),
    ALL_HEX_VALUES().borrow_u8_array()
);
Source§

type Err = String

The associated error which can be returned from parsing.
Source§

impl<const N: usize, const B: u8> PartialEq for ArrayOfBase<N, B>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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, const B: u8> Copy for ArrayOfBase<N, B>

Source§

impl<const N: usize, const B: u8> Eq for ArrayOfBase<N, B>

Source§

impl<const N: usize, const B: u8> StructuralPartialEq for ArrayOfBase<N, B>

Auto Trait Implementations§

§

impl<const N: usize, const B: u8> Freeze for ArrayOfBase<N, B>

§

impl<const N: usize, const B: u8> RefUnwindSafe for ArrayOfBase<N, B>

§

impl<const N: usize, const B: u8> Send for ArrayOfBase<N, B>

§

impl<const N: usize, const B: u8> Sync for ArrayOfBase<N, B>

§

impl<const N: usize, const B: u8> Unpin for ArrayOfBase<N, B>

§

impl<const N: usize, const B: u8> UnwindSafe for ArrayOfBase<N, B>

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