1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use serde::{Deserialize, Serialize};
pub use wasm_msgpack::timestamp::Timestamp;

use crate::error::Error;

#[doc(hidden)]
pub fn mp_serialize<T>(item: &T) -> std::result::Result<Vec<u8>, wasm_msgpack::encode::Error>
where
  T: ?Sized + Serialize,
{
  let mut buf = [0; 1024 * 100];
  let written = wasm_msgpack::encode::serde::to_array(item, &mut buf)?;
  Ok(buf[0..written].to_vec())
}

/// The standard function for serializing codec structs into a format that can be.
/// used for message exchange between actor and host. Use of any other function to.
/// serialize could result in breaking incompatibilities.
pub fn serialize<T>(item: &T) -> Result<Vec<u8>, crate::error::Error>
where
  T: ?Sized + Serialize,
{
  mp_serialize(item).map_err(Error::MsgPackEncode)
}

#[doc(hidden)]
pub fn mp_deserialize<'de, T: Deserialize<'de>>(buf: &'de [u8]) -> std::result::Result<T, wasm_msgpack::decode::Error> {
  wasm_msgpack::decode::from_slice(buf)
}

/// The standard function for de-serializing codec structs from a format suitable.
/// for message exchange between actor and host. Use of any other function to.
/// deserialize could result in breaking incompatibilities.
pub fn deserialize<'de, T: Deserialize<'de>>(buf: &'de [u8]) -> Result<T, crate::error::Error> {
  mp_deserialize(buf).map_err(Error::MsgPackDecode)
}