trouble_host/
pdu.rs

1use crate::Packet;
2
3pub(crate) struct Pdu<P> {
4    packet: P,
5    len: usize,
6}
7
8impl<P> Pdu<P> {
9    pub(crate) fn new(packet: P, len: usize) -> Self {
10        Self { packet, len }
11    }
12    pub(crate) fn len(&self) -> usize {
13        self.len
14    }
15    pub(crate) fn into_inner(self) -> P {
16        self.packet
17    }
18}
19
20impl<P: Packet> AsRef<[u8]> for Pdu<P> {
21    fn as_ref(&self) -> &[u8] {
22        &self.packet.as_ref()[..self.len]
23    }
24}
25
26impl<P: Packet> AsMut<[u8]> for Pdu<P> {
27    fn as_mut(&mut self) -> &mut [u8] {
28        &mut self.packet.as_mut()[..self.len]
29    }
30}
31
32/// Service Data Unit
33///
34/// A unit of payload that can be received or sent over an L2CAP channel.
35pub struct Sdu<P> {
36    pdu: Pdu<P>,
37}
38
39impl<P> Sdu<P> {
40    /// Create a new SDU using the allocated packet that has been pre-populated with data.
41    pub fn new(packet: P, len: usize) -> Self {
42        Self {
43            pdu: Pdu::new(packet, len),
44        }
45    }
46
47    pub(crate) fn from_pdu(pdu: Pdu<P>) -> Self {
48        Self { pdu }
49    }
50
51    /// Payload length.
52    pub fn len(&self) -> usize {
53        self.pdu.len()
54    }
55
56    /// Payload length.
57    pub fn is_empty(&self) -> bool {
58        self.pdu.len() == 0
59    }
60
61    /// Retrieve the inner packet.
62    pub fn into_inner(self) -> P {
63        self.pdu.into_inner()
64    }
65}
66
67impl<P: Packet> AsRef<[u8]> for Sdu<P> {
68    fn as_ref(&self) -> &[u8] {
69        self.pdu.as_ref()
70    }
71}
72
73impl<P: Packet> AsMut<[u8]> for Sdu<P> {
74    fn as_mut(&mut self) -> &mut [u8] {
75        self.pdu.as_mut()
76    }
77}