to_fixed_bytes

Function to_fixed_bytes 

Source
pub fn to_fixed_bytes<const N: usize, T>(
    value: &T,
) -> Result<FixedBytes<N>, Error>
where T: ?Sized + Encode<Binary>,
Expand description

Encode the given value to a fixed-size bytes using the default Encoding.

use musli::{Decode, Encode, FixedBytes};
use musli::packed;

#[derive(Decode, Encode)]
struct Person {
    name: String,
    age: u32,
}

let data: FixedBytes<128> = packed::to_fixed_bytes(&Person {
    name: "Aristotle".to_string(),
    age: 61,
})?;

let person: Person = packed::from_slice(&data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);