Skip to content

Commit

Permalink
♻️ zb: impl From<Socket> for BoxedSplit
Browse files Browse the repository at this point in the history
Simplify a bit the code.
  • Loading branch information
elmarco committed Feb 1, 2024
1 parent 53dd2b1 commit cc3620a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
20 changes: 10 additions & 10 deletions zbus/src/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl<'a> Builder<'a> {

/// Create a builder for connection that will use the given socket.
pub fn socket<S: Socket + 'static>(socket: S) -> Self {
Self::new(Target::Socket(Split::new_boxed(socket)))
Self::new(Target::Socket(socket.into()))
}

/// Specify the mechanisms to use during authentication.
Expand Down Expand Up @@ -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,
})
Expand Down
16 changes: 8 additions & 8 deletions zbus/src/connection/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()),
Expand All @@ -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()),
Expand Down
21 changes: 11 additions & 10 deletions zbus/src/connection/socket/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ pub struct Split<R: ReadHalf, W: WriteHalf> {
}

impl<R: ReadHalf, W: WriteHalf> Split<R, W> {
/// Create a new boxed `Split` from `socket`.
pub fn new_boxed<S: Socket<ReadHalf = R, WriteHalf = W>>(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
Expand Down Expand Up @@ -46,3 +36,14 @@ impl<R: ReadHalf, W: WriteHalf> Split<R, W> {

/// A boxed `Split`.
pub type BoxedSplit = Split<Box<dyn ReadHalf>, Box<dyn WriteHalf>>;

impl<S: Socket> From<S> for BoxedSplit {
fn from(socket: S) -> Self {
let split = socket.split();

Split {
read: Box::new(split.read),
write: Box::new(split.write),
}
}
}

0 comments on commit cc3620a

Please sign in to comment.