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
impl ByteArray
Sourcepub fn new_uninit() -> UninitByteArray
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
§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]);Sourcepub fn with_capacity(size_in_bytes: usize) -> Self
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
Sourcepub fn fill_zeros(self) -> Self
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])
Sourcepub fn fill_with(self, value: u8) -> Self
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]);
Sourcepub fn with_hex(self, hex_str: &str) -> Result<Self, ByteArrayError>
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]);Sourcepub fn with_bin(self, bin_str: &str) -> Result<Self, ByteArrayError>
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]);Sourcepub fn init_zeros(count: usize) -> Self
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
Sourcepub fn init_value(value: u8, count: usize) -> Self
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
Sourcepub fn as_bytes(&self) -> &[u8]
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]);Sourcepub fn get_padding(&self) -> ByteArrayOddWordPad
pub fn get_padding(&self) -> ByteArrayOddWordPad
returns the set padding
Source§impl ByteArray
impl ByteArray
Sourcepub fn iter(&self) -> ByteArrayIter<'_> ⓘ
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);
Sourcepub fn iter_mut(&mut self) -> ByteArrayIterMut<'_> ⓘ
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]);
Trait Implementations§
Source§impl BitAndAssign for ByteArray
impl BitAndAssign for ByteArray
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
&= operation. Read moreSource§impl BitOrAssign for ByteArray
impl BitOrAssign for ByteArray
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|= operation. Read moreSource§impl BitXorAssign for ByteArray
impl BitXorAssign for ByteArray
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
^= operation. Read moreSource§impl Debug for ByteArray
this is the insecure version of debug impl that leaks byte array data
impl Debug for ByteArray
this is the insecure version of debug impl that leaks byte array data
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
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§impl FromStr for ByteArray
impl FromStr for ByteArray
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
parses a &str as follows :
- does it start with a format identifier with
0x,0b, or0othen 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"
- The routines for the conversion here ignore underscore
- for all other cases defaults to
UTF-8as encoding and parses the slice as such
TODO add example with string slice lower than 2 chars