Skip to main content

leodos_protocols/network/
ptp.rs

1use crate::datalink::{DatalinkRead, DatalinkWrite};
2use crate::network::{NetworkRead, NetworkWrite};
3
4/// A point-to-point network layer that forwards directly to a datalink.
5pub struct PointToPoint<L> {
6    link: L,
7}
8
9impl<L> PointToPoint<L> {
10    /// Wraps a datalink in a point-to-point network layer.
11    pub fn new(link: L) -> Self {
12        Self { link }
13    }
14
15    /// Consumes the wrapper and returns the inner datalink.
16    pub fn into_inner(self) -> L {
17        self.link
18    }
19}
20
21impl<L: DatalinkWrite> NetworkWrite for PointToPoint<L> {
22    type Error = L::Error;
23
24    async fn write(&mut self, data: &[u8]) -> Result<(), Self::Error> {
25        DatalinkWrite::write(&mut self.link, data).await
26    }
27}
28
29impl<L: DatalinkRead> NetworkRead for PointToPoint<L> {
30    type Error = L::Error;
31
32    async fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error> {
33        DatalinkRead::read(&mut self.link, buffer).await
34    }
35}