[][src]Function ordcode::calc_size

pub fn calc_size<T: ?Sized, P>(value: &T, _params: P) -> Result<usize> where
    T: Serialize,
    P: SerializerParams

Calculate exact size of serialized data for a serde::Serialize value.

Useful for calculating exact size of serialized objects for buffer allocations. Calculation process is inexpensive, for fixed-size objects it evaluates to compile-time constant, or a few len() method calls for variable-size objects.


#[derive(serde_derive::Serialize)]
struct Foo(u16, String);
let foo = Foo(1, "abc".to_string());

let data_size = calc_size(&foo, params::AscendingOrder).unwrap();
assert_eq!(data_size, 6);