leodos_protocols/datalink/
mod.rs1use core::future::Future;
4
5pub mod framing;
7pub mod spp;
9pub mod link;
11pub mod reliability;
13pub mod security;
15
16pub trait DatalinkWrite {
20 type Error: core::error::Error;
22
23 fn write(&mut self, data: &[u8]) -> impl Future<Output = Result<(), Self::Error>>;
25}
26
27pub trait DatalinkRead {
29 type Error: core::error::Error;
31
32 fn read(&mut self, buffer: &mut [u8]) -> impl Future<Output = Result<usize, Self::Error>>;
34}
35
36pub trait Datalink {
39 type ReadError: core::error::Error;
41 type WriteError: core::error::Error;
43 type Reader<'a>: DatalinkRead<Error = Self::ReadError>
45 where
46 Self: 'a;
47 type Writer<'a>: DatalinkWrite<Error = Self::WriteError>
49 where
50 Self: 'a;
51
52 fn split(&mut self) -> (Self::Reader<'_>, Self::Writer<'_>);
54}
55
56impl<R: DatalinkRead, W: DatalinkWrite> Datalink for (R, W) {
57 type ReadError = R::Error;
58 type WriteError = W::Error;
59 type Reader<'a> = &'a mut R where Self: 'a;
60 type Writer<'a> = &'a mut W where Self: 'a;
61
62 fn split(&mut self) -> (&mut R, &mut W) {
63 (&mut self.0, &mut self.1)
64 }
65}
66
67impl<T: DatalinkRead + ?Sized> DatalinkRead for &mut T {
68 type Error = T::Error;
69
70 async fn read(
71 &mut self,
72 buffer: &mut [u8],
73 ) -> Result<usize, Self::Error> {
74 T::read(self, buffer).await
75 }
76}
77
78impl<T: DatalinkWrite + ?Sized> DatalinkWrite for &mut T {
79 type Error = T::Error;
80
81 async fn write(
82 &mut self,
83 data: &[u8],
84 ) -> Result<(), Self::Error> {
85 T::write(self, data).await
86 }
87}