crux_core/bridge/
formats.rs1use super::FfiFormat;
2
3#[derive(Debug)]
5pub struct BincodeFfiFormat;
6
7impl BincodeFfiFormat {
8 fn bincode_options() -> impl bincode::Options {
9 use bincode::Options;
10 bincode::DefaultOptions::new()
11 .with_fixint_encoding()
12 .allow_trailing_bytes()
13 }
14}
15
16impl FfiFormat for BincodeFfiFormat {
17 type Error = bincode::Error;
18
19 fn serialize<T: serde::Serialize>(buffer: &mut Vec<u8>, value: &T) -> Result<(), Self::Error> {
20 value.serialize(&mut bincode::Serializer::new(
21 buffer,
22 Self::bincode_options(),
23 ))
24 }
25
26 fn deserialize<'de, T: serde::Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, Self::Error> {
27 T::deserialize(&mut bincode::Deserializer::from_slice(
28 bytes,
29 Self::bincode_options(),
30 ))
31 }
32}
33
34#[derive(Debug)]
36pub struct JsonFfiFormat;
37
38impl FfiFormat for JsonFfiFormat {
39 type Error = serde_json::Error;
40
41 fn serialize<T: serde::Serialize>(buffer: &mut Vec<u8>, value: &T) -> Result<(), Self::Error> {
42 serde_json::to_writer(buffer, value)
43 }
44
45 fn deserialize<'de, T: serde::Deserialize<'de>>(bytes: &'de [u8]) -> Result<T, Self::Error> {
46 serde_json::from_slice(bytes)
47 }
48}