std_embedded_nal/
lib.rs

1//! This crate implements the [embedded-nal] network traits for operating systems that support the
2//! standard library's network.
3//!
4//! UDP and TCP sockets are plainly wrapped and should behave unsuspiciously.
5//!
6//! The DNS implementation is slightly incomplete, as the Rust standard library contains no
7//! provisions to turn IP addresses back into hosts; that call thus fails unconditionally.
8//!
9//! All implementations use `std::io::Error` as their error type.
10//!
11//! [embedded-nal]: https://crates.io/crates/embedded-nal
12
13mod conversion;
14mod dns;
15mod tcp;
16mod udp;
17
18/// The operating system's network stack, implementing ``embedded_nal::UdpFullStack`` and others.
19///
20/// The user may instantiate a stack using the `Stack::default()` function.
21///
22/// The stack can be cloned, as it is not a resource that needs any synchronization. This is not
23/// made implicit as Copy, though (although there's not technical reason not to). That is to alert
24/// users to the difficulties that'd arise when copying around a stack rather than using it through
25/// some mechanism of synchronization (which is generally required with ``embedded_nal`` since
26/// version 0.3).
27#[derive(Clone, Default)]
28pub struct Stack;
29
30#[deprecated(note = "Use Stack::default() instead.")]
31pub static STACK: Stack = Stack;
32
33/// An std::io::Error compatible error type returned when an operation is requested in the wrong
34/// sequence (where the "right" is create a socket, connect, any receive/send, and possibly close).
35#[derive(Debug)]
36struct OutOfOrder;
37
38impl std::fmt::Display for OutOfOrder {
39    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
40        write!(f, "Out of order operations requested")
41    }
42}
43
44impl std::error::Error for OutOfOrder {}
45
46impl<T> From<OutOfOrder> for std::io::Result<T> {
47    fn from(_: OutOfOrder) -> std::io::Result<T> {
48        Err(std::io::Error::new(
49            std::io::ErrorKind::NotConnected,
50            OutOfOrder,
51        ))
52    }
53}
54
55/// Socket
56enum SocketState<C, B> {
57    Building,
58    Connected(C),
59    Bound(B),
60}
61
62impl<C, B> SocketState<C, B> {
63    fn new() -> Self {
64        Self::Building
65    }
66
67    fn get_running(&mut self) -> std::io::Result<&mut C> {
68        match self {
69            SocketState::Connected(ref mut s) => Ok(s),
70            _ => OutOfOrder.into(),
71        }
72    }
73
74    fn get_bound(&mut self) -> std::io::Result<&mut B> {
75        match self {
76            SocketState::Bound(ref mut s) => Ok(s),
77            _ => OutOfOrder.into(),
78        }
79    }
80}
81
82impl<T> SocketState<T, T> {
83    fn get_any(&self) -> std::io::Result<&T> {
84        match self {
85            SocketState::Connected(ref s) => Ok(s),
86            SocketState::Bound(ref s) => Ok(s),
87            _ => OutOfOrder.into(),
88        }
89    }
90
91    fn get_any_mut(&mut self) -> std::io::Result<&mut T> {
92        match self {
93            SocketState::Connected(ref mut s) => Ok(s),
94            SocketState::Bound(ref mut s) => Ok(s),
95            _ => OutOfOrder.into(),
96        }
97    }
98}