to_bytes

Function to_bytes 

Source
pub fn to_bytes<T: Encodable + GetSize>(src: T) -> Result<Vec<u8>, Error>
Expand description

Converts the provided SV2 data type to a byte vector based on the SV2 encoding format.

Examples found in repository?
examples/encode_decode.rs (line 112)
103fn main() {
104    let expected = Test {
105        a: 456,
106        b: 9,
107        c: 67_u32.try_into().unwrap(),
108    };
109
110    // `to_bytes` serves as the entry point to the `binary_sv2` crate. It acts as a serializer that
111    // converts the struct into bytes.
112    let mut bytes = to_bytes(expected.clone()).unwrap();
113
114    // `from_bytes` is a deserializer that interprets the bytes and reconstructs the original
115    // struct.
116    let deserialized: Test = from_bytes(&mut bytes[..]).unwrap();
117
118    assert_eq!(deserialized, expected);
119}