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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use js_sys::Uint8Array;
use thiserror::Error;
use wasm_bindgen::prelude::*;
pub mod spsc;
#[derive(Debug, Clone, Error)]
#[error("expects {0} bytes more")]
pub struct Expects(pub u32);
pub trait Shareable: Sized {
type Error: std::error::Error;
fn to_bytes(&self) -> Result<Uint8Array, Self::Error>;
fn from(bytes: &Uint8Array) -> Result<Result<Self, Expects>, Self::Error>;
}
#[cfg(feature = "serde")]
impl<T> Shareable for T
where
for<'a> T: serde::Serialize + serde::Deserialize<'a>,
{
#[cfg(not(any(feature = "serde-bincode")))]
std::compile_error!("one of these features has to be enabled: serde-bincode");
#[cfg(feature = "serde-bincode")]
type Error = bincode::Error;
fn to_bytes(&self) -> Result<Uint8Array, Self::Error> {
#[cfg(feature = "serde-bincode")]
let mut result: Vec<u8> = (bincode::serialized_size(self)? as u32)
.to_ne_bytes()
.into();
#[cfg(feature = "serde-bincode")]
let mut encoded: Vec<u8> = bincode::serialize(self)?;
result.append(&mut encoded);
Ok(Uint8Array::from(&result[..]))
}
fn from(bytes: &Uint8Array) -> Result<Result<Self, Expects>, Self::Error> {
if bytes.byte_length() == 0 {
return Ok(Err(Expects(4)));
}
if bytes.byte_length() >= 4 {
let mut data = vec![0; bytes.byte_length() as usize];
bytes.copy_to(&mut data);
let size = u32::from_ne_bytes([data[0], data[1], data[2], data[3]]);
if bytes.byte_length() == 4 {
return Ok(Err(Expects(4 + size)));
}
#[cfg(feature = "serde-bincode")]
return Ok(Ok(bincode::deserialize::<Self>(&data[4..])?));
}
#[cfg(feature = "serde-bincode")]
Err(Box::new(bincode::ErrorKind::Custom(
"unexpected data".to_string(),
)))
}
}