Function rocket::serde::msgpack::to_vec

source ·
pub fn to_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 named representation.

The named representation represents structs as maps with field names.

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 = &[
    130, 169, 102, 114, 97, 109, 101, 119, 111, 114, 107, 166, 82, 111,
    99, 107, 101, 116, 165, 115, 116, 97, 114, 115, 5
];

let data: Data = msgpack::from_slice(bytes).unwrap();
let byte_vec = msgpack::to_vec(&data).unwrap();
assert_eq!(bytes, &byte_vec[..]);

Errors

Serialization fails if T’s Serialize implementation fails.