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() -> Self
pub fn new() -> Self
Standard constructor, use Self::with_capacity if you already know the capacity of your bytes for performance
reasons
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
Sourcepub fn fill_zeros(self) -> Self
👎Deprecated since 0.3.3: This will be renamed to with_zeros at a future version
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::model::ByteArray;
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::with_capacity(7)
.fill_zeros();
assert_eq!(arr.as_bytes(),[0u8,0,0,0,0,0,0])
Sourcepub fn fill_with(self, value: u8) -> Self
👎Deprecated since 0.3.3: This will be renamed to with_value at a future version
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::model::ByteArray;
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::with_capacity(7)
.fill_with(5);
assert_eq!(arr.as_bytes(),[5u8,5,5,5,5,5,5]);
Sourcepub fn from_hex(hex_str: &str) -> Result<Self, ByteArrayError>
pub fn from_hex(hex_str: &str) -> Result<Self, ByteArrayError>
Create a byte array from a hex string (without 0x prefix)
§Example
use byte_array_ops::ByteArray;
let arr = ByteArray::from_hex("deadbeef")?;
assert_eq!(arr.as_bytes(), [0xde, 0xad, 0xbe, 0xef]);Sourcepub fn from_bin(bin_str: &str) -> Result<Self, ByteArrayError>
pub fn from_bin(bin_str: &str) -> Result<Self, ByteArrayError>
Create a byte array from a binary string (without 0b prefix)
§Example
use byte_array_ops::ByteArray;
let arr = ByteArray::from_bin("1010010")?;
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::model::ByteArray;
let arr : ByteArray = "0xff2569".parse().unwrap();
let slice = arr.as_bytes();
assert_eq!(slice, [0xff,0x25,0x69]);Sourcepub fn try_extend(self, other: ByteArray) -> Result<Self, ByteArrayError>
pub fn try_extend(self, other: ByteArray) -> Result<Self, ByteArrayError>
Chained constructor helper for concatening ByteArrays together into one while ensuring to unintended reallocations happens
to avoid data remnance.
Preallocate the correct size with ByteArray::with_capacity then unintended reallocations might happen)
§Capacity allocation caveat
this function adjusts the inner bytes capacity according to the sum of the lengths of the actual data of both ByteArrays,
if you want to preserve the capacity to be the sum of the total capacities of both arrays; then use [ByteArray::with_extend_preserve_cap]
§Errors
This function fails if a reallocation happens with crate::errors::ByteArraySecurityError::DataRemnanceRisk
§Example
use byte_array_ops::ByteArray;
let arr1 = ByteArray::from_hex("dead")?;
let arr2 = ByteArray::from_hex("beef")?;
let result = arr1.try_extend(arr2)?;
assert_eq!(result.as_bytes(), [0xde, 0xad, 0xbe, 0xef]);Sourcepub fn try_extend_with_preserve_cap(
self,
other: ByteArray,
) -> Result<Self, ByteArrayError>
pub fn try_extend_with_preserve_cap( self, other: ByteArray, ) -> Result<Self, ByteArrayError>
Same as ByteArray::try_extend but instead of reserving actual lengths, this reserves the
sum of capacities of both bytearrays
§Warning
For Bytearrays with very large capacities but whose actual length is very small this tends to be
very inefficient and might allocate too much memory on constrained environments.
For these cases use ByteArray::try_extend chained , this might be safer at the cost of more
secure reallocations
§Example
use byte_array_ops::ByteArray;
let arr1 = ByteArray::with_capacity(100);
let arr2 = ByteArray::from_hex("ff")?;
let result = arr1.try_extend_with_preserve_cap(arr2)?;
// Result has the data from arr2
assert_eq!(result.as_bytes(), [0xff]);
assert_eq!(result.len(), 1);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::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::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
impl ByteArray
Sourcepub unsafe fn as_ptr(&self) -> *const u8
pub unsafe fn as_ptr(&self) -> *const u8
raw pointer access for cases where the data is passed over an ffi interface and needs to be read
§Safety
Do
NOTuse only if you must. useByteArray::as_bytesorByteArray::as_refas a safe alternative
This function returns the raw pointer to the inner type as is and avoids any checks it is thus marked unsafe
Sourcepub unsafe fn as_mut_ptr(&mut self) -> *mut u8
pub unsafe fn as_mut_ptr(&mut self) -> *mut u8
raw mutable pointer access for cases where the data is passed over an ffi interface and needs to be mutated
§Safety
Do
NOTuse only if you must. useByteArray::as_mutas a safe alternative
This function returns the raw pointer to the inner type as is and avoids any checks it is thus marked unsafe
Source§impl ByteArray
impl ByteArray
Sourcepub fn get(&self, index: usize) -> Option<&u8>
pub fn get(&self, index: usize) -> Option<&u8>
Returns a reference to the byte at the given index, or None if out of bounds
Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Truncate function similar to alloc::vec::Vec::truncate but zeroizes the discarded
portions before truncating to avoid leaking sensitive data. the len represents the
new ByteArray length in bytes.
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 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