Trait FixedBytesSliceExt
pub trait FixedBytesSliceExt {
// Required methods
fn as_flattened(&self) -> &[u8] ⓘ;
fn as_flattened_mut(&mut self) -> &mut [u8] ⓘ;
}Expand description
Extension trait for flattening a slice of FixedBytes to a byte slice.
This mirrors the standard library’s as_flattened and as_flattened_mut methods for
&[[T; N]].
Required Methods§
fn as_flattened(&self) -> &[u8] ⓘ
fn as_flattened(&self) -> &[u8] ⓘ
Takes a &[FixedBytes<N>] and flattens it to a &[u8].
§Panics
This panics if the length of the resulting slice would overflow a usize.
This is only possible when N == 0, which tends to be irrelevant in practice.
§Examples
use alloy_primitives::{FixedBytes, FixedBytesSliceExt};
let arr = [FixedBytes::<4>::new([1, 2, 3, 4]), FixedBytes::new([5, 6, 7, 8])];
assert_eq!(arr.as_flattened(), &[1, 2, 3, 4, 5, 6, 7, 8]);fn as_flattened_mut(&mut self) -> &mut [u8] ⓘ
fn as_flattened_mut(&mut self) -> &mut [u8] ⓘ
Takes a &mut [FixedBytes<N>] and flattens it to a &mut [u8].
§Panics
This panics if the length of the resulting slice would overflow a usize.
This is only possible when N == 0, which tends to be irrelevant in practice.
§Examples
use alloy_primitives::{FixedBytes, FixedBytesSliceExt};
fn add_one(slice: &mut [u8]) {
for b in slice {
*b = b.wrapping_add(1);
}
}
let mut arr = [FixedBytes::<4>::new([1, 2, 3, 4]), FixedBytes::new([5, 6, 7, 8])];
add_one(arr.as_flattened_mut());
assert_eq!(arr[0].as_slice(), &[2, 3, 4, 5]);