webrtc_util/marshal/
mod.rs

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