Skip to main content

edge_nal/stack/
raw.rs

1//! Factory traits for creating raw sockets on embedded devices
2
3use embedded_io_async::ErrorType;
4
5use crate::raw::{RawReceive, RawSend};
6use crate::Readable;
7
8/// This trait is implemented by raw sockets that can be split into separate `send` and `receive` halves that can operate
9/// independently from each other (i.e., a full-duplex connection)
10pub trait RawSplit: ErrorType {
11    type Receive<'a>: RawReceive<Error = Self::Error> + Readable<Error = Self::Error>
12    where
13        Self: 'a;
14    type Send<'a>: RawSend<Error = Self::Error>
15    where
16        Self: 'a;
17
18    fn split(&mut self) -> (Self::Receive<'_>, Self::Send<'_>);
19}
20
21impl<T> RawSplit for &mut T
22where
23    T: RawSplit,
24{
25    type Receive<'a>
26        = T::Receive<'a>
27    where
28        Self: 'a;
29    type Send<'a>
30        = T::Send<'a>
31    where
32        Self: 'a;
33
34    fn split(&mut self) -> (Self::Receive<'_>, Self::Send<'_>) {
35        (**self).split()
36    }
37}
38
39/// This trait is implemented by raw socket stacks. The trait allows the underlying driver to
40/// construct multiple connections that implement the raw socket traits
41pub trait RawBind {
42    /// Error type returned on socket creation failure
43    type Error: embedded_io_async::Error;
44
45    /// The socket type returned by the stack
46    type Socket<'a>: RawReceive<Error = Self::Error>
47        + RawSend<Error = Self::Error>
48        + RawSplit<Error = Self::Error>
49        + Readable<Error = Self::Error>
50    where
51        Self: 'a;
52
53    /// Create a raw socket
54    ///
55    /// On most operating systems, creating a raw socket requires admin privileges.
56    async fn bind(&self) -> Result<Self::Socket<'_>, Self::Error>;
57}
58
59impl<T> RawBind for &T
60where
61    T: RawBind,
62{
63    type Error = T::Error;
64
65    type Socket<'a>
66        = T::Socket<'a>
67    where
68        Self: 'a;
69
70    async fn bind(&self) -> Result<Self::Socket<'_>, Self::Error> {
71        (*self).bind().await
72    }
73}
74
75impl<T> RawBind for &mut T
76where
77    T: RawBind,
78{
79    type Error = T::Error;
80
81    type Socket<'a>
82        = T::Socket<'a>
83    where
84        Self: 'a;
85
86    async fn bind(&self) -> Result<Self::Socket<'_>, Self::Error> {
87        (**self).bind().await
88    }
89}