pub struct FixedBytes<const N: usize> { /* private fields */ }Expand description
Generic fixed-size byte array with const generic size.
Provides a type-safe wrapper around fixed-size arrays with useful utility methods for encoding, comparison, and serialization.
§Type Safety
Different sizes create incompatible types at compile time:
let hash32 = FixedBytes::<32>::new([0u8; 32]);
let hash64 = FixedBytes::<64>::new([0u8; 64]);
// hash32 == hash64; // Compile error: different typesImplementations§
Source§impl<const N: usize> FixedBytes<N>
impl<const N: usize> FixedBytes<N>
Sourcepub fn from_slice(slice: &[u8]) -> Option<Self>
pub fn from_slice(slice: &[u8]) -> Option<Self>
Create from a byte slice. Returns None if the slice length doesn’t match.
§Example
let bytes = &[1, 2, 3, 4];
let fixed = FixedBytes::<4>::from_slice(bytes).unwrap();
assert_eq!(fixed.as_bytes(), bytes);Sourcepub fn from_hex(hex: &str) -> Result<Self, String>
pub fn from_hex(hex: &str) -> Result<Self, String>
Create from a hexadecimal string.
§Errors
Returns error if the hex string is invalid or has wrong length.
Sourcepub fn as_bytes_mut(&mut self) -> &mut [u8; N]
pub fn as_bytes_mut(&mut self) -> &mut [u8; N]
Get the byte array as a mutable slice.
Trait Implementations§
Source§impl<const N: usize> Clone for FixedBytes<N>
impl<const N: usize> Clone for FixedBytes<N>
Source§fn clone(&self) -> FixedBytes<N>
fn clone(&self) -> FixedBytes<N>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<const N: usize> Debug for FixedBytes<N>
impl<const N: usize> Debug for FixedBytes<N>
Source§impl<const N: usize> Default for FixedBytes<N>
impl<const N: usize> Default for FixedBytes<N>
Source§impl<'de, const N: usize> Deserialize<'de> for FixedBytes<N>
impl<'de, const N: usize> Deserialize<'de> for FixedBytes<N>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<const N: usize> Display for FixedBytes<N>
impl<const N: usize> Display for FixedBytes<N>
Source§impl<const N: usize> From<FixedBytes<N>> for [u8; N]
impl<const N: usize> From<FixedBytes<N>> for [u8; N]
Source§fn from(fixed: FixedBytes<N>) -> Self
fn from(fixed: FixedBytes<N>) -> Self
Converts to this type from the input type.
Source§impl<const N: usize> Hash for FixedBytes<N>
impl<const N: usize> Hash for FixedBytes<N>
Source§impl<const N: usize> PartialEq for FixedBytes<N>
impl<const N: usize> PartialEq for FixedBytes<N>
Source§impl<const N: usize> Serialize for FixedBytes<N>
impl<const N: usize> Serialize for FixedBytes<N>
impl<const N: usize> Copy for FixedBytes<N>
impl<const N: usize> Eq for FixedBytes<N>
impl<const N: usize> StructuralPartialEq for FixedBytes<N>
Auto Trait Implementations§
impl<const N: usize> Freeze for FixedBytes<N>
impl<const N: usize> RefUnwindSafe for FixedBytes<N>
impl<const N: usize> Send for FixedBytes<N>
impl<const N: usize> Sync for FixedBytes<N>
impl<const N: usize> Unpin for FixedBytes<N>
impl<const N: usize> UnwindSafe for FixedBytes<N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more