Function tycho::to_bytes[][src]

pub fn to_bytes<S: Serialize>(o: S) -> TychoResult<Vec<u8>>

Serialize a serde serializable object into tycho bytes. (requires serde_support)

use serde::Serialize;
use tycho::{Element, to_bytes};

// Create a serializable serde structure.
#[derive(Serialize)]
pub struct Example {
    foo: String
}

// Instantiate serializable object.
let data = Example { foo: "Hi".to_string() };

// Convert serializable serde structure to bytes.
let bytes = to_bytes(data).unwrap();

assert_eq!(bytes, vec![5, 9, 102, 111, 111, 0, 1, 2, 2, 72, 105])