Repository: quartiq/minimq
Pull request #15 (2020-09-01 13:56:38)

Replace ad-hoc std-based NAL with std-embedded-nal
Granted, std-embedded-nal is quite immature yet, but hey it works :-)

(If you like this but feel held back by the git= dependency, I'm happy to upload a 0.0.1, but didn't yet as it might get some vetting in the course of this PR.)


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 511b66a..3d15b59 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -28,3 +28,4 @@ logging = ["log"]
 
 [dev-dependencies]
 env_logger = "0.7"
+std-embedded-nal = "=0.0.1"


Test patch:
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index c62090c..1d070c0 100644
--- a/tests/integration_test.rs
+++ b/tests/integration_test.rs
@@ -1,132 +1,12 @@
 use minimq::{consts, MqttClient, Property, QoS};
 
-use std::cell::RefCell;
-use std::io::{self, Read, Write};
-use std::net::{self, TcpStream};
-
-use embedded_nal::{self, nb, IpAddr, Ipv4Addr, SocketAddr};
-
-struct StandardStack {
-    stream: RefCell<Option<TcpStream>>,
-    mode: RefCell<embedded_nal::Mode>,
-}
-
-impl StandardStack {
-    pub fn new() -> StandardStack {
-        StandardStack {
-            stream: RefCell::new(None),
-            mode: RefCell::new(embedded_nal::Mode::Blocking),
-        }
-    }
-}
-
-impl embedded_nal::TcpStack for StandardStack {
-    type Error = io::Error;
-
-    type TcpSocket = ();
-
-    fn open(&self, mode: embedded_nal::Mode) -> Result<Self::TcpSocket, Self::Error> {
-        self.mode.replace(mode);
-        Ok(())
-    }
-
-    fn connect(
-        &self,
-        _socket: Self::TcpSocket,
-        remote: SocketAddr,
-    ) -> Result<Self::TcpSocket, Self::Error> {
-        let ip = match remote.ip() {
-            IpAddr::V4(addr) => net::IpAddr::V4(net::Ipv4Addr::new(
-                addr.octets()[0],
-                addr.octets()[1],
-                addr.octets()[2],
-                addr.octets()[3],
-            )),
-            IpAddr::V6(addr) => net::IpAddr::V6(net::Ipv6Addr::new(
-                addr.segments()[0],
-                addr.segments()[1],
-                addr.segments()[2],
-                addr.segments()[3],
-                addr.segments()[4],
-                addr.segments()[5],
-                addr.segments()[6],
-                addr.segments()[7],
-            )),
-        };
-
-        let remote = net::SocketAddr::new(ip, remote.port());
-
-        let stream = TcpStream::connect(remote).unwrap();
-
-        match *self.mode.borrow() {
-            embedded_nal::Mode::NonBlocking => stream.set_nonblocking(true)?,
-            embedded_nal::Mode::Blocking => stream.set_nonblocking(false)?,
-            embedded_nal::Mode::Timeout(t) => {
-                stream.set_read_timeout(Some(std::time::Duration::from_secs(t.into())))?;
-                stream.set_write_timeout(Some(std::time::Duration::from_secs(t.into())))?;
-            }
-        }
-        self.stream.replace(Some(stream));
-
-        Ok(())
-    }
-
-    fn is_connected(&self, _socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
-        Ok(self.stream.borrow().is_some())
-    }
-
-    fn write(
-        &self,
-        _socket: &mut Self::TcpSocket,
-        buffer: &[u8],
-    ) -> nb::Result<usize, Self::Error> {
-        match &mut *self.stream.borrow_mut() {
-            Some(stream) => match stream.write(buffer) {
-                Ok(len) => Ok(len),
-                Err(e) => {
-                    if e.kind() == std::io::ErrorKind::WouldBlock {
-                        Err(nb::Error::WouldBlock)
-                    } else {
-                        Err(nb::Error::Other(e))
-                    }
-                }
-            },
-            None => Ok(0),
-        }
-    }
-
-    fn read(
-        &self,
-        _socket: &mut Self::TcpSocket,
-        buffer: &mut [u8],
-    ) -> nb::Result<usize, Self::Error> {
-        match &mut *self.stream.borrow_mut() {
-            Some(stream) => match stream.read(buffer) {
-                Ok(len) => Ok(len),
-                Err(e) => {
-                    if e.kind() == std::io::ErrorKind::WouldBlock {
-                        Err(nb::Error::WouldBlock)
-                    } else {
-                        Err(nb::Error::Other(e))
-                    }
-                }
-            },
-            None => Ok(0),
-        }
-    }
-
-    fn close(&self, _socket: Self::TcpSocket) -> Result<(), Self::Error> {
-        self.stream.replace(None).unwrap();
-
-        Ok(())
-    }
-}
+use embedded_nal::{self, IpAddr, Ipv4Addr};
 
 #[test]
 fn main() -> std::io::Result<()> {
     env_logger::init();
 
-    let stack = StandardStack::new();
+    let stack = std_embedded_nal::STACK.clone();
     let localhost = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
     let mut client =
         MqttClient::<consts::U256, _>::new(localhost, "IntegrationTest", stack).unwrap();