Trait instrs::Serialize

source ·
pub trait Serialize: Sized {
    fn from_bytes<S>(b: &mut &[u8]) -> Result<Self, Error>
    where
        S: Size
; fn into_bytes<S>(&self, b: &mut Vec<u8, Global>) -> Result<(), Error>
    where
        S: Size
; }
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"));

Required Methods§

Implementations on Foreign Types§

This function will panic. Make sure to convert to an owned String instead

Implementors§