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>
impl<const N: usize, const B: u8> ArrayOfBase<N, B>
Sourcepub fn try_new(v: [u8; N]) -> Result<Self, Error>
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:
- Invalid value in supplied array
Error::InvalidValue
§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
]
);Sourcepub fn try_from_vec_exact(v: &[u8]) -> Result<Self, Error>
pub fn try_from_vec_exact(v: &[u8]) -> Result<Self, Error>
Create from Vec of u8 numbers
§Errors
Will error if:
- Too many elements in the supplied vector
Error::Oversized - Too few, but not 0, elements in the supplied vector
Error::Undersized - Empty vector supplied
Error::Empty - Invalid value in supplied array
Error::InvalidValue
§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
]
);Sourcepub fn try_from_vec_pad(v: &[u8]) -> Result<Self, Error>
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:
- Too many elements in the supplied vector
Error::Oversized - Empty vector supplied
Error::Empty - Invalid value in supplied array
Error::InvalidValue
§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
]
);Sourcepub fn unwrap(self) -> [u8; N]
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)Sourcepub fn borrow_u8_array(&self) -> &[u8; N]
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)Sourcepub fn trim<const L: usize>(&self) -> ArrayOfBase<L, B>
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>
impl<const N: usize> ArrayOfBase<N, 16>
Sourcepub fn from_u8_array<const I: usize>(array: [u8; I]) -> ArrayOfHex<N>
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
Ivalue is not halfNthe function will panic. - If
Nis 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>
impl<const N: usize, const B: u8> Clone for ArrayOfBase<N, B>
Source§fn clone(&self) -> ArrayOfBase<N, B>
fn clone(&self) -> ArrayOfBase<N, B>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const N: usize> Display for ArrayOfBase<N, 16>
impl<const N: usize> Display for ArrayOfBase<N, 16>
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Output the contents of ArrayOfHex as a hexadecimal string
§Restrictions
This function only works if:
- The base
Bof theArrayOfBaseis16orArrayOfHexis used
§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>
impl<const N: usize> FromStr for ArrayOfBase<N, 16>
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Parse a hexadecimal string into ArrayOfHex
§Restrictions
This function only works if:
- The base
Bof theArrayOfBaseis16orArrayOfHexis used
§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()
);