Repository: quartiq/minimq
Pull request #48 (2021-08-10 14:42:39)

Replace ad-hoc std-based NAL with std-embedded-nal
This is doing b0b60b2e again after 2f4dd69f reintroduced a local version
due to std-embedded-nal lagging behind embedded-nal development.

---

Thanks to your [PR](https://gitlab.com/chrysn/std-embedded-nal/-/merge_requests/2), std-embedded-nal is again at state-of-the-art embedded-nal level.

Alternatively to this, if std-embedded-nal is too slow to adapt, it should probably be removed from dev-dependencies.

(As an alternative to the alternative, given that maintaining the code removed here is very similar to the steps in maintaining std-embedded-nal, I'd be happy to invite you to become comaintainer of that crate).


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4f2e7ec..2ab2dc2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,10 @@
 
 This document describes the changes to Minimq between releases.
 
+# Unreleased
+
+* Updating to `std-embedded-nal` v0.1 (dev dependency only; now again used for integration tests)
+
 # Version 0.3.0
 Version 0.3.0 was published on 2021-08-06
 
diff --git a/Cargo.toml b/Cargo.toml
index 8e2e09a..e727242 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -30,4 +30,4 @@ logging = ["log"]
 
 [dev-dependencies]
 env_logger = "0.7"
-std-embedded-nal = "=0.0.1"
+std-embedded-nal = "0.1"


Test patch:
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index 9c8ceb6..e871ea8 100644
--- a/tests/integration_test.rs
+++ b/tests/integration_test.rs
@@ -1,86 +1,12 @@
 use minimq::{Minimq, Property, QoS};
-use std::io::{Read, Write};
 
-use embedded_nal::{self, nb, IpAddr, Ipv4Addr, SocketAddr};
-
-#[derive(Copy, Clone, Debug)]
-struct STACK;
-
-pub struct TcpSocket {
-    state: Option<std::net::TcpStream>,
-}
-
-impl TcpSocket {
-    pub fn new() -> Self {
-        Self { state: None }
-    }
-}
-
-fn to_nb(e: std::io::Error) -> nb::Error<std::io::Error> {
-    use std::io::ErrorKind::{TimedOut, WouldBlock};
-
-    match e.kind() {
-        WouldBlock | TimedOut => nb::Error::WouldBlock,
-        _ => e.into(),
-    }
-}
-
-impl embedded_nal::TcpClientStack for STACK {
-    type Error = std::io::Error;
-    type TcpSocket = TcpSocket;
-
-    fn socket(&mut self) -> Result<TcpSocket, Self::Error> {
-        Ok(TcpSocket::new())
-    }
-
-    fn connect(
-        &mut self,
-        socket: &mut TcpSocket,
-        remote: SocketAddr,
-    ) -> nb::Result<(), Self::Error> {
-        let octets: [u8; 4] = match remote.ip() {
-            IpAddr::V4(addr) => addr.octets(),
-            _ => panic!("Unsupported IP version"),
-        };
-
-        let soc = std::net::TcpStream::connect(std::net::SocketAddr::new(
-            std::net::IpAddr::V4(std::net::Ipv4Addr::from(octets)),
-            remote.port(),
-        ))?;
-        soc.set_nonblocking(true)?;
-        socket.state.replace(soc);
-
-        Ok(())
-    }
-
-    fn is_connected(&mut self, socket: &TcpSocket) -> Result<bool, Self::Error> {
-        Ok(socket.state.is_some())
-    }
-
-    fn send(&mut self, socket: &mut TcpSocket, buffer: &[u8]) -> nb::Result<usize, Self::Error> {
-        let socket = socket.state.as_mut().unwrap();
-        socket.write(buffer).map_err(to_nb)
-    }
-
-    fn receive(
-        &mut self,
-        socket: &mut TcpSocket,
-        buffer: &mut [u8],
-    ) -> nb::Result<usize, Self::Error> {
-        let socket = socket.state.as_mut().unwrap();
-        socket.read(buffer).map_err(to_nb)
-    }
-
-    fn close(&mut self, _socket: TcpSocket) -> Result<(), Self::Error> {
-        Ok(())
-    }
-}
+use embedded_nal::{self, IpAddr, Ipv4Addr};
 
 #[test]
 fn main() -> std::io::Result<()> {
     env_logger::init();
 
-    let stack = STACK.clone();
+    let stack = std_embedded_nal::Stack::default();
     let localhost = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
     let mut mqtt = Minimq::<_, 256>::new(localhost, "", stack).unwrap();