Skip to content

Commit 744a878

Browse files
author
germaniuss
committed
♻️ zb: Move UnixSocket connection code to unix.rs
Move UnixSocket connection code to unix.rs from mod.rs for better readability and maintainability.
1 parent 8128bb3 commit 744a878

2 files changed

Lines changed: 91 additions & 58 deletions

File tree

zbus/src/address/transport/mod.rs

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@ use async_io::Async;
1212
use std::collections::HashMap;
1313
#[cfg(not(feature = "tokio"))]
1414
use std::net::TcpStream;
15-
#[cfg(unix)]
16-
use std::os::unix::net::{SocketAddr, UnixStream};
1715
#[cfg(feature = "tokio")]
1816
use tokio::net::TcpStream;
1917
#[cfg(feature = "tokio-vsock")]
2018
use tokio_vsock::VsockStream;
21-
#[cfg(windows)]
22-
use uds_windows::UnixStream;
2319
#[cfg(all(feature = "vsock", not(feature = "tokio")))]
2420
use vsock::VsockStream;
2521
#[cfg(unix)]
@@ -33,7 +29,7 @@ use std::{
3329
};
3430

3531
mod unix;
36-
pub use unix::{Unix, UnixSocket};
32+
pub use unix::{Unix, UnixSocket, UnixStream};
3733
mod tcp;
3834
pub use tcp::{Tcp, TcpTransportFamily};
3935
#[cfg(windows)]
@@ -55,8 +51,6 @@ pub use ibus::Ibus;
5551
#[path = "vsock.rs"]
5652
// Gotta rename to avoid name conflict with the `vsock` crate.
5753
mod vsock_transport;
58-
#[cfg(target_os = "linux")]
59-
use std::os::linux::net::SocketAddrExt;
6054
#[cfg(any(
6155
all(feature = "vsock", not(feature = "tokio")),
6256
feature = "tokio-vsock"
@@ -102,58 +96,12 @@ impl Transport {
10296
#[cfg_attr(any(unix, windows), async_recursion::async_recursion)]
10397
pub(super) async fn connect(self) -> Result<Stream> {
10498
match self {
99+
#[allow(unused_variables)]
105100
Transport::Unix(unix) => {
106-
// This is a `path` in case of Windows until uds_windows provides the needed API:
107-
// https://github.com/haraldh/rust_uds_windows/issues/14
108-
let addr = match unix.take_path() {
109-
#[cfg(unix)]
110-
UnixSocket::File(path) => SocketAddr::from_pathname(path)?,
111-
#[cfg(windows)]
112-
UnixSocket::File(path) => path,
113-
#[cfg(target_os = "linux")]
114-
UnixSocket::Abstract(name) => {
115-
SocketAddr::from_abstract_name(name.as_encoded_bytes())?
116-
}
117-
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
118-
// You can't connect to a unix:dir.
119-
return Err(Error::Unsupported);
120-
}
121-
};
122-
let stream = crate::Task::spawn_blocking(
123-
move || -> Result<_> {
124-
#[cfg(unix)]
125-
let stream = UnixStream::connect_addr(&addr)?;
126-
#[cfg(windows)]
127-
let stream = UnixStream::connect(addr)?;
128-
stream.set_nonblocking(true)?;
129-
130-
Ok(stream)
131-
},
132-
"unix stream connection",
133-
)
134-
.await??;
135-
#[cfg(not(feature = "tokio"))]
136-
{
137-
Async::new(stream)
138-
.map(Stream::Unix)
139-
.map_err(|e| Error::InputOutput(e.into()))
140-
}
141-
142-
#[cfg(feature = "tokio")]
143-
{
144-
#[cfg(unix)]
145-
{
146-
tokio::net::UnixStream::from_std(stream)
147-
.map(Stream::Unix)
148-
.map_err(|e| Error::InputOutput(e.into()))
149-
}
150-
151-
#[cfg(not(unix))]
152-
{
153-
let _ = stream;
154-
Err(Error::Unsupported)
155-
}
156-
}
101+
#[cfg(all(not(unix), feature = "tokio"))]
102+
return Err(Error::Unsupported);
103+
#[cfg(any(unix, not(feature = "tokio")))]
104+
unix.connect().await.map(Stream::Unix)
157105
}
158106
#[cfg(unix)]
159107
Transport::Unixexec(unixexec) => unixexec.connect().await.map(Stream::Unixexec),

zbus/src/address/transport/unix.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[allow(unused_imports)]
2+
use crate::{Error, Result};
3+
14
#[cfg(target_os = "linux")]
25
use std::ffi::OsString;
36
use std::{
@@ -9,6 +12,18 @@ use std::{
912
#[cfg(unix)]
1013
use super::encode_percents;
1114

15+
#[cfg(not(feature = "tokio"))]
16+
use async_io::Async;
17+
18+
#[cfg(target_os = "linux")]
19+
use std::os::linux::net::SocketAddrExt;
20+
#[cfg(unix)]
21+
use std::os::unix::net::SocketAddr;
22+
#[cfg(unix)]
23+
pub use std::os::unix::net::UnixStream;
24+
#[cfg(windows)]
25+
pub use uds_windows::UnixStream;
26+
1227
/// A Unix domain socket transport in a D-Bus address.
1328
#[derive(Clone, Debug, PartialEq, Eq)]
1429
pub struct Unix {
@@ -31,6 +46,34 @@ impl Unix {
3146
self.path
3247
}
3348

49+
#[cfg(all(windows, not(feature = "tokio")))]
50+
fn take_addr(self) -> Result<PathBuf> {
51+
// This is a `path` in case of Windows until uds_windows provides the needed API:
52+
// https://github.com/haraldh/rust_uds_windows/issues/14
53+
match self.take_path() {
54+
UnixSocket::File(path) => Ok(path),
55+
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
56+
// You can't connect to a unix:dir.
57+
Err(Error::Unsupported)
58+
}
59+
}
60+
}
61+
62+
#[cfg(unix)]
63+
fn take_addr(self) -> Result<SocketAddr> {
64+
match self.take_path() {
65+
UnixSocket::File(path) => Ok(SocketAddr::from_pathname(path)?),
66+
#[cfg(target_os = "linux")]
67+
UnixSocket::Abstract(name) => {
68+
Ok(SocketAddr::from_abstract_name(name.as_encoded_bytes())?)
69+
}
70+
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
71+
// You can't connect to a unix:dir.
72+
Err(Error::Unsupported)
73+
}
74+
}
75+
}
76+
3477
pub(super) fn from_options(opts: std::collections::HashMap<&str, &str>) -> crate::Result<Self> {
3578
let path = opts.get("path");
3679
let abs = opts.get("abstract");
@@ -55,6 +98,48 @@ impl Unix {
5598

5699
Ok(Self::new(path))
57100
}
101+
102+
#[cfg(not(feature = "tokio"))]
103+
pub(super) async fn connect(self) -> Result<Async<UnixStream>> {
104+
let addr = self.take_addr()?;
105+
106+
let stream = crate::Task::spawn_blocking(
107+
move || -> Result<_> {
108+
#[cfg(unix)]
109+
let stream = UnixStream::connect_addr(&addr)?;
110+
#[cfg(windows)]
111+
let stream = UnixStream::connect(addr)?;
112+
stream.set_nonblocking(true)?;
113+
114+
Ok(stream)
115+
},
116+
"unix stream connection",
117+
)
118+
.await??;
119+
120+
Async::new(stream).map_err(|e| Error::InputOutput(e.into()))
121+
}
122+
123+
#[cfg(all(unix, feature = "tokio"))]
124+
pub(super) async fn connect(self) -> Result<tokio::net::UnixStream> {
125+
let addr = self.take_addr()?;
126+
127+
let stream = crate::Task::spawn_blocking(
128+
move || -> Result<_> {
129+
#[cfg(unix)]
130+
let stream = UnixStream::connect_addr(&addr)?;
131+
#[cfg(windows)]
132+
let stream = UnixStream::connect(addr)?;
133+
stream.set_nonblocking(true)?;
134+
135+
Ok(stream)
136+
},
137+
"unix stream connection",
138+
)
139+
.await??;
140+
141+
tokio::net::UnixStream::from_std(stream).map_err(|e| Error::InputOutput(e.into()))
142+
}
58143
}
59144

60145
impl Display for Unix {

0 commit comments

Comments
 (0)