Mercurial > crates > systemd-socket
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 20:2b78a483f84b | 21:f6334887e3c8 |
|---|---|
| 12 //! You only need to change the code to use `SocketAddr::bind()` instead of `TcpListener::bind()` for binding. | 12 //! You only need to change the code to use `SocketAddr::bind()` instead of `TcpListener::bind()` for binding. |
| 13 //! | 13 //! |
| 14 //! You also don't need to worry about conditional compilation to ensure OS compatibility. | 14 //! You also don't need to worry about conditional compilation to ensure OS compatibility. |
| 15 //! This crate handles that for you by disabling systemd on non-linux systems. | 15 //! This crate handles that for you by disabling systemd on non-linux systems. |
| 16 //! | 16 //! |
| 17 //! Further, the crate also provides methods for binding `tokio` 0.2, 0.3, and `async_std` sockets if the appropriate features are | 17 //! Further, the crate also provides methods for binding `tokio` 1.0, 0.2, 0.3, and `async_std` sockets if the appropriate features are |
| 18 //! activated. | 18 //! activated. |
| 19 //! | 19 //! |
| 20 //! ## Example | 20 //! ## Example |
| 21 //! | 21 //! |
| 22 //! ```no_run | 22 //! ```no_run |
| 45 //! * `enable_systemd` - on by default, the existence of this feature can allow your users to turn | 45 //! * `enable_systemd` - on by default, the existence of this feature can allow your users to turn |
| 46 //! off systemd support if they don't need it. Note that it's already disabled on non-linux | 46 //! off systemd support if they don't need it. Note that it's already disabled on non-linux |
| 47 //! systems, so you don't need to care about that. | 47 //! systems, so you don't need to care about that. |
| 48 //! * `serde` - implements `serde::Deserialize` for `SocketAddr` | 48 //! * `serde` - implements `serde::Deserialize` for `SocketAddr` |
| 49 //! * `parse_arg` - implements `parse_arg::ParseArg` for `SocketAddr` | 49 //! * `parse_arg` - implements `parse_arg::ParseArg` for `SocketAddr` |
| 50 //! * `tokio` - adds `bind_tokio` method to `SocketAddr` (tokio 1.0) | |
| 50 //! * `tokio_0_2` - adds `bind_tokio_0_2` method to `SocketAddr` | 51 //! * `tokio_0_2` - adds `bind_tokio_0_2` method to `SocketAddr` |
| 51 //! * `tokio_0_3` - adds `bind_tokio_0_3` method to `SocketAddr` | 52 //! * `tokio_0_3` - adds `bind_tokio_0_3` method to `SocketAddr` |
| 52 //! * `async_std` - adds `bind_async_std` method to `SocketAddr` | 53 //! * `async_std` - adds `bind_async_std` method to `SocketAddr` |
| 53 //! | 54 //! |
| 54 //! ## MSRV | 55 //! ## MSRV |
| 177 Ok(socket) => Ok(socket), | 178 Ok(socket) => Ok(socket), |
| 178 Err(error) => Err(BindErrorInner::BindOrResolvFailed { addr, error, }.into()), | 179 Err(error) => Err(BindErrorInner::BindOrResolvFailed { addr, error, }.into()), |
| 179 }, | 180 }, |
| 180 SocketAddrInner::Systemd(socket_name) => Self::get_systemd(socket_name, true).map(|(socket, _)| socket), | 181 SocketAddrInner::Systemd(socket_name) => Self::get_systemd(socket_name, true).map(|(socket, _)| socket), |
| 181 SocketAddrInner::SystemdNoPrefix(socket_name) => Self::get_systemd(socket_name, false).map(|(socket, _)| socket), | 182 SocketAddrInner::SystemdNoPrefix(socket_name) => Self::get_systemd(socket_name, false).map(|(socket, _)| socket), |
| 183 } | |
| 184 } | |
| 185 | |
| 186 /// Creates `tokio::net::TcpListener` | |
| 187 /// | |
| 188 /// To be specific, it binds the socket or converts systemd socket to `tokio` 1.0 socket. | |
| 189 /// | |
| 190 /// This method either `binds` the socket, if the address was provided or uses systemd socket | |
| 191 /// if the socket name was provided. | |
| 192 #[cfg(feature = "tokio")] | |
| 193 pub async fn bind_tokio(self) -> Result<tokio::net::TcpListener, TokioBindError> { | |
| 194 match self.0 { | |
| 195 SocketAddrInner::Ordinary(addr) => match tokio::net::TcpListener::bind(addr).await { | |
| 196 Ok(socket) => Ok(socket), | |
| 197 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())), | |
| 198 }, | |
| 199 SocketAddrInner::WithHostname(addr) => match tokio::net::TcpListener::bind(addr.as_str()).await { | |
| 200 Ok(socket) => Ok(socket), | |
| 201 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())), | |
| 202 }, | |
| 203 SocketAddrInner::Systemd(socket_name) => { | |
| 204 let (socket, addr) = Self::get_systemd(socket_name, true)?; | |
| 205 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into()) | |
| 206 }, | |
| 207 SocketAddrInner::SystemdNoPrefix(socket_name) => { | |
| 208 let (socket, addr) = Self::get_systemd(socket_name, false)?; | |
| 209 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into()) | |
| 210 }, | |
| 182 } | 211 } |
| 183 } | 212 } |
| 184 | 213 |
| 185 /// Creates `tokio::net::TcpListener` | 214 /// Creates `tokio::net::TcpListener` |
| 186 /// | 215 /// |
