From 079f584a5edb4303f4e77c6f00344d62fa3a0136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 31 Jan 2024 14:01:03 +0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=20zb:=20impl=20From=20for=20BoxedSplit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplify a bit the code. --- zbus/src/connection/builder.rs | 20 ++++++++++---------- zbus/src/connection/handshake.rs | 16 ++++++++-------- zbus/src/connection/socket/split.rs | 21 +++++++++++---------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/zbus/src/connection/builder.rs b/zbus/src/connection/builder.rs index 4fc53006c..f9ce72db5 100644 --- a/zbus/src/connection/builder.rs +++ b/zbus/src/connection/builder.rs @@ -161,7 +161,7 @@ impl<'a> Builder<'a> { /// Create a builder for connection that will use the given socket. pub fn socket(socket: S) -> Self { - Self::new(Target::Socket(Split::new_boxed(socket))) + Self::new(Target::Socket(socket.into())) } /// Specify the mechanisms to use during authentication. @@ -461,28 +461,28 @@ impl<'a> Builder<'a> { // once. Ok(match self.target.take().unwrap() { #[cfg(not(feature = "tokio"))] - Target::UnixStream(stream) => Split::new_boxed(Async::new(stream)?), + Target::UnixStream(stream) => Async::new(stream)?.into(), #[cfg(all(unix, feature = "tokio"))] - Target::UnixStream(stream) => Split::new_boxed(stream), + Target::UnixStream(stream) => stream.into(), #[cfg(all(not(unix), feature = "tokio"))] Target::UnixStream(_) => return Err(Error::Unsupported), #[cfg(not(feature = "tokio"))] - Target::TcpStream(stream) => Split::new_boxed(Async::new(stream)?), + Target::TcpStream(stream) => Async::new(stream)?.into(), #[cfg(feature = "tokio")] - Target::TcpStream(stream) => Split::new_boxed(stream), + Target::TcpStream(stream) => stream.into(), #[cfg(all(feature = "vsock", not(feature = "tokio")))] - Target::VsockStream(stream) => Split::new_boxed(Async::new(stream)?), + Target::VsockStream(stream) => Async::new(stream)?.into(), #[cfg(feature = "tokio-vsock")] - Target::VsockStream(stream) => Split::new_boxed(stream), + Target::VsockStream(stream) => stream.into(), Target::Address(address) => match address.connect().await? { #[cfg(any(unix, not(feature = "tokio")))] - address::transport::Stream::Unix(stream) => Split::new_boxed(stream), - address::transport::Stream::Tcp(stream) => Split::new_boxed(stream), + address::transport::Stream::Unix(stream) => stream.into(), + address::transport::Stream::Tcp(stream) => stream.into(), #[cfg(any( all(feature = "vsock", not(feature = "tokio")), feature = "tokio-vsock" ))] - address::transport::Stream::Vsock(stream) => Split::new_boxed(stream), + address::transport::Stream::Vsock(stream) => stream.into(), }, Target::Socket(stream) => stream, }) diff --git a/zbus/src/connection/handshake.rs b/zbus/src/connection/handshake.rs index b3b33a3fa..0d512cb8c 100644 --- a/zbus/src/connection/handshake.rs +++ b/zbus/src/connection/handshake.rs @@ -1045,7 +1045,7 @@ mod tests { use super::*; - use crate::{connection::socket::Split, Guid, Socket}; + use crate::{Guid, Socket}; fn create_async_socket_pair() -> (impl AsyncWrite + Socket, impl AsyncWrite + Socket) { // Tokio needs us to call the sync function from async context. :shrug: @@ -1071,9 +1071,9 @@ mod tests { let (p0, p1) = create_async_socket_pair(); let guid = OwnedGuid::from(Guid::generate()); - let client = ClientHandshake::new(Split::new_boxed(p0), None, Some(guid.clone())); + let client = ClientHandshake::new(p0.into(), None, Some(guid.clone())); let server = ServerHandshake::new( - Split::new_boxed(p1), + p1.into(), guid, Some(Uid::effective().into()), None, @@ -1097,7 +1097,7 @@ mod tests { fn pipelined_handshake() { let (mut p0, p1) = create_async_socket_pair(); let server = ServerHandshake::new( - Split::new_boxed(p1), + p1.into(), Guid::generate().into(), Some(Uid::effective().into()), None, @@ -1126,7 +1126,7 @@ mod tests { fn separate_external_data() { let (mut p0, p1) = create_async_socket_pair(); let server = ServerHandshake::new( - Split::new_boxed(p1), + p1.into(), Guid::generate().into(), Some(Uid::effective().into()), None, @@ -1153,7 +1153,7 @@ mod tests { fn missing_external_data() { let (mut p0, p1) = create_async_socket_pair(); let server = ServerHandshake::new( - Split::new_boxed(p1), + p1.into(), Guid::generate().into(), Some(Uid::effective().into()), None, @@ -1171,7 +1171,7 @@ mod tests { fn anonymous_handshake() { let (mut p0, p1) = create_async_socket_pair(); let server = ServerHandshake::new( - Split::new_boxed(p1), + p1.into(), Guid::generate().into(), Some(Uid::effective().into()), Some(vec![AuthMechanism::Anonymous].into()), @@ -1189,7 +1189,7 @@ mod tests { fn separate_anonymous_data() { let (mut p0, p1) = create_async_socket_pair(); let server = ServerHandshake::new( - Split::new_boxed(p1), + p1.into(), Guid::generate().into(), Some(Uid::effective().into()), Some(vec![AuthMechanism::Anonymous].into()), diff --git a/zbus/src/connection/socket/split.rs b/zbus/src/connection/socket/split.rs index dc26082c1..403b97568 100644 --- a/zbus/src/connection/socket/split.rs +++ b/zbus/src/connection/socket/split.rs @@ -8,16 +8,6 @@ pub struct Split { } impl Split { - /// Create a new boxed `Split` from `socket`. - pub fn new_boxed>(socket: S) -> BoxedSplit { - let split = socket.split(); - - Split { - read: Box::new(split.read), - write: Box::new(split.write), - } - } - /// Reference to the read half. pub fn read(&self) -> &R { &self.read @@ -46,3 +36,14 @@ impl Split { /// A boxed `Split`. pub type BoxedSplit = Split, Box>; + +impl From for BoxedSplit { + fn from(socket: S) -> Self { + let split = socket.split(); + + Split { + read: Box::new(split.read), + write: Box::new(split.write), + } + } +}