rtc_shared/marshal/
mod.rs

1use bytes::{Buf, BytesMut};
2
3use crate::error::{Error, Result};
4
5pub trait MarshalSize {
6    fn marshal_size(&self) -> usize;
7}
8
9pub trait Marshal: MarshalSize {
10    fn marshal_to(&self, buf: &mut [u8]) -> Result<usize>;
11
12    fn marshal(&self) -> Result<BytesMut> {
13        let l = self.marshal_size();
14        let mut buf = BytesMut::with_capacity(l);
15        buf.resize(l, 0);
16        let n = self.marshal_to(&mut buf)?;
17        if n != l {
18            Err(Error::Other(format!(
19                "marshal_to output size {n}, but expect {l}"
20            )))
21        } else {
22            Ok(buf)
23        }
24    }
25}
26
27pub trait Unmarshal: MarshalSize {
28    fn unmarshal<B>(buf: &mut B) -> Result<Self>
29    where
30        Self: Sized,
31        B: Buf;
32}