Trait instrs_core::Serialize
source · pub trait Serialize: Sized {
fn from_bytes<S: Size>(b: &mut &[u8]) -> Result<Self, Error>;
fn into_bytes<S: Size>(&self, b: &mut Vec<u8>) -> Result<(), Error>;
}
Expand description
An item that can be Serialized into bytes.
Examples
// `S` parameter is needed in all cases
assert_eq!(u32::from_bytes::<u8>(&mut [3,0,0,0].as_slice(), Ok(3u32)));
// Passing more bytes than needed is not an error
assert_eq!(bool::from_bytes::<u8>(&mut [1,0,0,0,0].as_slice()), Ok(true));
assert_eq!([char; 4]::from_bytes::<u8>(&mut [65,0,0,0, 66,0,0,0, 67,0,0,0, 68,0,0,0].as_slice()), ['A', 'B', 'C', 'D']);
assert_eq!(String::from_bytes::<u8>(&mut [4, 65,66,67,68].as_slice()), String::from("ABCD"));