changeset 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
files Cargo.toml README.md src/error.rs src/lib.rs
diffstat 4 files changed, 34 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Cargo.toml	Sat Jul 13 14:19:29 2024 +0200
+++ b/Cargo.toml	Sat Jul 13 15:09:13 2024 +0200
@@ -30,6 +30,7 @@
 serde_str_helpers = { version = "0.1.2", optional = true }
 parse_arg = { version = "0.1.4", optional = true }
 lazy_static = "1.4.0"
+tokio = { package = "tokio", version = "1.0.0", optional = true, features = ["net"] }
 tokio_0_2 = { package = "tokio", version = "0.2", optional = true, features = ["tcp", "dns"] }
 tokio_0_3 = { package = "tokio", version = "0.3", optional = true, features = ["net"] }
 async-std = { version = "1.7.0", optional = true }
--- a/README.md	Sat Jul 13 14:19:29 2024 +0200
+++ b/README.md	Sat Jul 13 15:09:13 2024 +0200
@@ -49,6 +49,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_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`
--- a/src/error.rs	Sat Jul 13 14:19:29 2024 +0200
+++ b/src/error.rs	Sat Jul 13 15:09:13 2024 +0200
@@ -81,7 +81,7 @@
 ///
 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert.
 /// This error type expresses this possibility.
-#[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))]
+#[cfg(any(feature = "tokio", feature = "tokio_0_2", feature = "tokio_0_3"))]
 #[derive(Debug, Error)]
 #[error(transparent)]
 pub enum TokioBindError {
@@ -96,7 +96,7 @@
 /// As opposed to `std` and `async_std` sockets, tokio sockets can fail to convert.
 /// This error type encapsulates conversion error together with additional information so that it
 /// can be displayed nicely. The encapsulation also allows for future-proofing.
-#[cfg(any(feature = "tokio_0_2", feature = "tokio_0_3"))]
+#[cfg(any(feature = "tokio", feature = "tokio_0_2", feature = "tokio_0_3"))]
 #[derive(Debug, Error)]
 #[error("failed to convert std socket {addr} into tokio socket")]
 pub struct TokioConversionError {
--- 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