xref: /webrtc/sctp/src/param/param_random.rs (revision ffe74184)
1 use super::{param_header::*, param_type::*, *};
2 
3 use bytes::{Bytes, BytesMut};
4 
5 #[derive(Default, Debug, Clone, PartialEq)]
6 pub(crate) struct ParamRandom {
7     pub(crate) random_data: Bytes,
8 }
9 
10 impl fmt::Display for ParamRandom {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result11     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12         write!(f, "{} {:?}", self.header(), self.random_data)
13     }
14 }
15 
16 impl Param for ParamRandom {
header(&self) -> ParamHeader17     fn header(&self) -> ParamHeader {
18         ParamHeader {
19             typ: ParamType::Random,
20             value_length: self.value_length() as u16,
21         }
22     }
23 
unmarshal(raw: &Bytes) -> Result<Self>24     fn unmarshal(raw: &Bytes) -> Result<Self> {
25         let header = ParamHeader::unmarshal(raw)?;
26         let random_data =
27             raw.slice(PARAM_HEADER_LENGTH..PARAM_HEADER_LENGTH + header.value_length());
28         Ok(ParamRandom { random_data })
29     }
30 
marshal_to(&self, buf: &mut BytesMut) -> Result<usize>31     fn marshal_to(&self, buf: &mut BytesMut) -> Result<usize> {
32         self.header().marshal_to(buf)?;
33         buf.extend(self.random_data.clone());
34         Ok(buf.len())
35     }
36 
value_length(&self) -> usize37     fn value_length(&self) -> usize {
38         self.random_data.len()
39     }
40 
clone_to(&self) -> Box<dyn Param + Send + Sync>41     fn clone_to(&self) -> Box<dyn Param + Send + Sync> {
42         Box::new(self.clone())
43     }
44 
as_any(&self) -> &(dyn Any + Send + Sync)45     fn as_any(&self) -> &(dyn Any + Send + Sync) {
46         self
47     }
48 }
49