ByteArray

Struct ByteArray 

Source
pub struct ByteArray { /* private fields */ }
Expand description

Debug derive and Display is intentionally left out to avoid any intentional data leakages through formatters

Implementations§

Source§

impl ByteArray

Source

pub fn new_uninit() -> UninitByteArray

This constructor is usually needed when a custom odd array padding direction must be set. creates a new empty byte array without reserving memory. this might not be the most efficient option. For large byte arrays use ByteArray::with_capacity instead

§Returns

UninitByteArray

§Note

Use ByteArray::default or ByteArray::with_capacity to get a ByteArray without intermediate structs if you do not require setting the odd word padding

§Example
use core::str::FromStr;

use byte_array_ops::byte_array::model::{ByteArray, ByteArrayOddWordPad};

let arr = ByteArray::new_uninit()
        .with_odd_pad_dir(ByteArrayOddWordPad::LeftPadding)
        .with_hex("ffef48a").expect("failed to convert");

assert_eq!(arr.as_bytes(),[0x0f,0xfe,0xf4,0x8a]);
Source

pub fn with_capacity(size_in_bytes: usize) -> Self

reserves memory for a certain number of bytes for efficiency purposes uses the default Byte padding direction

Source

pub fn fill_zeros(self) -> Self

fills the byte array with zeros to capacity this is usually only useful in the rare case where an odd word padding needs to be set in all other cases use ByteArray::init_zeros. This function does nothing if the ByteArray capacity has not been set (is 0)

§Example
use byte_array_ops::byte_array::model::{ByteArray, ByteArrayOddWordPad};
let arr = ByteArray::default().fill_zeros(); // does nothing on an array with zero capacity
assert!(arr.as_bytes().is_empty());

let arr = ByteArray::with_capacity(5).fill_zeros();
assert_eq!(arr.as_bytes(),[0u8,0,0,0,0]);

// or in the rare cases where a different odd word padding is set

let arr = ByteArray::new_uninit()
                .with_capacity(7)
                .with_odd_pad_dir(ByteArrayOddWordPad::LeftPadding)
                .fill_zeros();

assert_eq!(arr.as_bytes(),[0u8,0,0,0,0,0,0])

Source

pub fn fill_with(self, value: u8) -> Self

fills the byte array with the given value to capacity this is usually only useful in the rare case where an odd word padding needs to be set in all other cases use ByteArray::init_value.

§Note

This function does nothing (noop) if the ByteArray capacity has not been set (is 0)

§Example
use byte_array_ops::byte_array::model::{ByteArray, ByteArrayOddWordPad};
let arr = ByteArray::default().fill_zeros(); // does nothing on an array with zero capacity
assert!(arr.as_bytes().is_empty());

let arr = ByteArray::with_capacity(5).fill_with(126);
assert_eq!(arr.as_bytes(),[126u8,126,126,126,126]);

// or in the rare cases where a different odd word padding is set

let arr = ByteArray::new_uninit()
                .with_capacity(7)
                .with_odd_pad_dir(ByteArrayOddWordPad::LeftPadding)
                .fill_with(5);

assert_eq!(arr.as_bytes(),[5u8,5,5,5,5,5,5]);

Source

pub fn with_hex(self, hex_str: &str) -> Result<Self, ByteArrayError>

chaining function to create a byte array from hex

TODO make this more resilient by accepting 0x too

§Example
use byte_array_ops::byte_array::model::ByteArray;
let arr = ByteArray::default()
        .with_hex("deadbeef").expect("failed to convert hex string");

assert_eq!(arr.as_bytes(), [0xde, 0xad, 0xbe, 0xef]);
Source

pub fn with_bin(self, bin_str: &str) -> Result<Self, ByteArrayError>

chaining function to create a byte array from a binary representation

§Example
use byte_array_ops::byte_array::model::ByteArray;
let arr = ByteArray::default()
        .with_bin("1010010").expect("failed to convert binary string");

assert_eq!(arr.as_bytes(), [0x52]);
Source

pub fn init_zeros(count: usize) -> Self

initialize the array with a certain amount of zeros internally this creates the byte array representation with vec![0u8;count] the rest is default initialized

Source

pub fn init_value(value: u8, count: usize) -> Self

initialize the array with a certain amount of value internally this creates the byte array representation with vec![value;count] the rest is default initialized

Source

pub fn as_bytes(&self) -> &[u8]

returns a slices to the interior bytes

§NOTE

There is another method that provides a zero-cost move of the interior bytes using the Vec::from::<ByteArray> implementation. Please check the ByteArray’s From<ByteArray> implementation documentation

§Example
use byte_array_ops::byte_array::model::ByteArray;

let arr : ByteArray = "0xff2569".parse().unwrap();

let slice = arr.as_bytes();

assert_eq!(slice, [0xff,0x25,0x69]);
§Alternative (Zero-cost move)
use byte_array_ops::byte_array::model::ByteArray;
let arr : ByteArray = "0b11101111".parse().unwrap();

assert_eq!(arr.as_bytes(), [0xef]);

let moved_data : Vec<u8> = arr.into();
assert_eq!(moved_data, vec![0xef]);
Source

pub fn get_padding(&self) -> ByteArrayOddWordPad

returns the set padding

Source

pub fn len(&self) -> usize

returns the number of bytes in the array TODO add example

Source

pub fn is_empty(&self) -> bool

returns true if there are no bytes in the array TODO example

Source§

impl ByteArray

Source

pub fn get(&self, index: usize) -> Option<&u8>

Source§

impl ByteArray

Source

pub fn iter(&self) -> ByteArrayIter<'_>

Returns an iterator over the bytes.

§Example
use byte_array_ops::byte_array::model::ByteArray;

let arr: ByteArray = "0xdeadbeef".parse().unwrap();
let mut iter = arr.iter();
assert_eq!(iter.next(), Some(&0xde));
assert_eq!(iter.next(),Some(&0xad));
assert_eq!(iter.next(),Some(&0xbe));
assert_eq!(iter.next(),Some(&0xef));
assert_eq!(iter.next(),None);
Source

pub fn iter_mut(&mut self) -> ByteArrayIterMut<'_>

Returns an iterator that allows modifying each byte.

§Example
use byte_array_ops::byte_array::model::ByteArray;

let mut arr: ByteArray = "0xff00ff".parse().unwrap();
let mut iter_mut = arr.iter_mut();
iter_mut.next();
let mut byte = iter_mut.next().unwrap();
*byte = 0xee;
assert_eq!(arr.as_bytes(),[0x0ff,0xee,0xff]);
Source§

impl ByteArray

Source

pub fn to_vec(self) -> Vec<u8>

This transforms the bytearray into an owned vector an is thus unavailable in hardned modes Allowing this in hardened modes means giving over control of resource sanitization to user which would not guarantee a proper purge TODO add security handbook readme

Trait Implementations§

Source§

impl AsRef<[u8]> for ByteArray

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl BitAnd for ByteArray

Source§

type Output = ByteArray

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Self) -> Self::Output

Performs the & operation. Read more
Source§

impl BitAndAssign for ByteArray

Source§

fn bitand_assign(&mut self, rhs: Self)

Performs the &= operation. Read more
Source§

impl BitOr for ByteArray

Source§

type Output = ByteArray

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Self) -> Self::Output

Performs the | operation. Read more
Source§

impl BitOrAssign for ByteArray

Source§

fn bitor_assign(&mut self, rhs: Self)

Performs the |= operation. Read more
Source§

impl BitXor for ByteArray

Source§

type Output = ByteArray

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Self) -> Self::Output

Performs the ^ operation. Read more
Source§

impl BitXorAssign for ByteArray

Source§

fn bitxor_assign(&mut self, rhs: Self)

Performs the ^= operation. Read more
Source§

impl Clone for ByteArray

Source§

fn clone(&self) -> ByteArray

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 Debug for ByteArray

this is the insecure version of debug impl that leaks byte array data

Source§

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

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

impl Default for ByteArray

Source§

fn default() -> ByteArray

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

impl Display for ByteArray

this is insecure as it might exposes internal byte array data for example in system logs and is thus only available in insecure modes. If the user wants to do it nonetheless he shall override the default display provider TODO

Source§

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

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

impl From<&[u8]> for ByteArray

Source§

fn from(value: &[u8]) -> Self

converts a byte slice into our Byte array, odd word padding is not needed here as arrays are always aligned TODO document this as potentially expensive copy

Source§

impl<const N: usize> From<&[u8; N]> for ByteArray

Source§

fn from(value: &[u8; N]) -> Self

converts a byte slice into our Byte array, odd word padding is not needed here as arrays are always aligned TODO document this as potentially expensive copy

Source§

impl<const N: usize> From<[u8; N]> for ByteArray

Source§

fn from(value: [u8; N]) -> Self

Converts to this type from the input type.
Source§

impl From<ByteArray> for Vec<u8>

Source§

fn from(arr: ByteArray) -> Self

This conversion and ownership transfer is only available in non-hardened modes

Source§

impl From<Vec<u8>> for ByteArray

Source§

fn from(bytes: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for ByteArray

Source§

fn from(value: u8) -> Self

Converts to this type from the input type.
Source§

impl FromStr for ByteArray

Source§

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

parses a &str as follows :

  • does it start with a format identifier with 0x, 0b, or 0o then parses in hex, binary or octal respectively (case insensitive)
    • The routines for the conversion here ignore underscore _ allowing its use as a separator for example "0b1110_0010_0110"
  • for all other cases defaults to UTF-8 as encoding and parses the slice as such

TODO add example with string slice lower than 2 chars

Source§

type Err = ByteArrayError

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

impl Index<usize> for ByteArray

Source§

fn index(&self, index: usize) -> &Self::Output

index accessor for ByteArray

§Panics

When out of bound use ByteArray::get instead if you want a checked getter

Source§

type Output = u8

The returned type after indexing.
Source§

impl IndexMut<usize> for ByteArray

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Mutable index accessor for ByteArray

§Panics

When out of bound use ByteArray::get instead if you want a checked getter

Source§

impl<'a> IntoIterator for &'a ByteArray

Source§

type Item = &'a u8

The type of the elements being iterated over.
Source§

type IntoIter = ByteArrayIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut ByteArray

Source§

type Item = &'a mut u8

The type of the elements being iterated over.
Source§

type IntoIter = ByteArrayIterMut<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Not for ByteArray

Source§

type Output = ByteArray

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl PartialEq for ByteArray

Source§

fn eq(&self, other: &ByteArray) -> 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 Eq for ByteArray

Source§

impl StructuralPartialEq for ByteArray

Auto Trait Implementations§

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.