Skip to main content

serialized_size

Function serialized_size 

Source
pub fn serialized_size<T: ?Sized + Serialize>(value: &T) -> Result<u64, Error>
Expand description

Computes the exact number of bytes that to_writer would produce for a value, without writing or buffering anything.

The value is serialized through the regular serializer into a counting sink, so the result is exact by construction (including preferred float widths, bignums, tags and indefinite-length containers) and no memory is allocated.

let value = ("hello", 42u64, vec![1u8, 2, 3]);
let size = cbor2::serialized_size(&value).unwrap();
assert_eq!(size as usize, cbor2::to_vec(&value).unwrap().len());