pub fn to_compact_vec<T>(value: &T) -> Result<Vec<u8>, Error>
where T: Serialize + ?Sized,
Available on crate feature msgpack only.
Expand description

Serialize a T into a MessagePack byte vector with compact representation.

The compact representation represents structs as arrays.

Always use MsgPack to serialize MessagePack response data.

§Example

use rocket::serde::{Deserialize, Serialize, msgpack};

#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
struct Data<'r> {
    framework: &'r str,
    stars: usize,
}

let bytes = &[146, 166, 82, 111, 99, 107, 101, 116, 5];
let data: Data = msgpack::from_slice(bytes).unwrap();
let byte_vec = msgpack::to_compact_vec(&data).unwrap();
assert_eq!(bytes, &byte_vec[..]);

§Errors

Serialization fails if T’s Serialize implementation fails.