diff src/lib.rs @ 21:f6334887e3c8

Support `tokio` 1.0 Tokio 1.0 was already out for a while and this adds the missing support for it. Deprecation of 0.2 and 0.3 is planned but they are staying for now.
author Martin Habovstiak <martin.habovstiak@gmail.com>
date Sat, 13 Jul 2024 15:09:13 +0200
parents 2b78a483f84b
children f33e2c048104
line wrap: on
line diff
--- a/src/lib.rs	Sat Jul 13 14:19:29 2024 +0200
+++ b/src/lib.rs	Sat Jul 13 15:09:13 2024 +0200
@@ -14,7 +14,7 @@
 //! You also don't need to worry about conditional compilation to ensure OS compatibility.
 //! This crate handles that for you by disabling systemd on non-linux systems.
 //!
-//! Further, the crate also provides methods for binding `tokio` 0.2, 0.3, and `async_std` sockets if the appropriate features are
+//! Further, the crate also provides methods for binding `tokio` 1.0, 0.2, 0.3, and `async_std` sockets if the appropriate features are
 //! activated.
 //! 
 //! ## Example
@@ -47,6 +47,7 @@
 //!   systems, so you don't need to care about that.
 //! * `serde` - implements `serde::Deserialize` for `SocketAddr`
 //! * `parse_arg` - implements `parse_arg::ParseArg` for `SocketAddr`
+//! * `tokio` - adds `bind_tokio` method to `SocketAddr` (tokio 1.0)
 //! * `tokio_0_2` - adds `bind_tokio_0_2` method to `SocketAddr`
 //! * `tokio_0_3` - adds `bind_tokio_0_3` method to `SocketAddr`
 //! * `async_std` - adds `bind_async_std` method to `SocketAddr`
@@ -184,6 +185,34 @@
 
     /// Creates `tokio::net::TcpListener`
     ///
+    /// To be specific, it binds the socket or converts systemd socket to `tokio` 1.0 socket.
+    ///
+    /// This method either `binds` the socket, if the address was provided or uses systemd socket
+    /// if the socket name was provided.
+    #[cfg(feature = "tokio")]
+    pub async fn bind_tokio(self) -> Result<tokio::net::TcpListener, TokioBindError> {
+        match self.0 {
+            SocketAddrInner::Ordinary(addr) => match tokio::net::TcpListener::bind(addr).await {
+                Ok(socket) => Ok(socket),
+                Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())),
+            },
+            SocketAddrInner::WithHostname(addr) => match tokio::net::TcpListener::bind(addr.as_str()).await {
+                Ok(socket) => Ok(socket),
+                Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())),
+            },
+            SocketAddrInner::Systemd(socket_name) => {
+                let (socket, addr) = Self::get_systemd(socket_name, true)?;
+                socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
+            },
+            SocketAddrInner::SystemdNoPrefix(socket_name) => {
+                let (socket, addr) = Self::get_systemd(socket_name, false)?;
+                socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into())
+            },
+        }
+    }
+
+    /// Creates `tokio::net::TcpListener`
+    ///
     /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.2 socket.
     ///
     /// This method either `binds` the socket, if the address was provided or uses systemd socket