From a69ca5d4e2fe2719465e7648baecee972b11c23a Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Fri, 22 Apr 2022 12:56:19 +0200 Subject: [PATCH] Add universal `listen` function This function accepts anything that can be converted into `service_binding::Listener` and that includes standard library's `UnixListener` and `TcpListener`. This makes the `listen` method one universal function that should be preferred. Since `listen` does not expose any Tokio 0.x types and `service_binding` has no dependencies this feature has minimal weight. Signed-off-by: Wiktor Kwapisiewicz --- ssh-agent-lib/Cargo.toml | 2 +- ssh-agent-lib/src/agent.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ssh-agent-lib/Cargo.toml b/ssh-agent-lib/Cargo.toml index 2bb37a7..807b17c 100644 --- a/ssh-agent-lib/Cargo.toml +++ b/ssh-agent-lib/Cargo.toml @@ -14,7 +14,7 @@ edition = "2021" [dependencies] byteorder = "1.2.7" serde = {version = "1.0.87", features = ["derive"]} - +service-binding = "0.1.1" bytes = { version = "0.4.11", optional = true } futures = { version = "0.1.25", optional = true } log = { version = "0.4.6", optional = true } diff --git a/ssh-agent-lib/src/agent.rs b/ssh-agent-lib/src/agent.rs index 57d3de7..50c7b8d 100644 --- a/ssh-agent-lib/src/agent.rs +++ b/ssh-agent-lib/src/agent.rs @@ -105,4 +105,22 @@ pub trait Agent: 'static + Sync + Send + Sized { let socket = TcpListener::bind(&addr.parse::()?)?; Ok(tokio::run(handle_clients!(self, socket))) } + + #[allow(clippy::unit_arg)] + fn listen(self, socket: T) -> Result<(), Box> + where + T: Into, + { + let socket = socket.into(); + match socket { + service_binding::Listener::Unix(listener) => { + let listener = UnixListener::from_std(listener, &Default::default())?; + Ok(tokio::run(handle_clients!(self, listener))) + } + service_binding::Listener::Tcp(listener) => { + let listener = TcpListener::from_std(listener, &Default::default())?; + Ok(tokio::run(handle_clients!(self, listener))) + } + } + } }