Skip to content

Commit d603542

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 be57c55 commit d603542

2 files changed

Lines changed: 72 additions & 64 deletions

File tree

zbus/src/address/transport/mod.rs

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,13 @@ mod unixexec;
2323
#[cfg(unix)]
2424
pub use unixexec::Unixexec;
2525

26-
#[cfg(all(unix, not(feature = "tokio")))]
27-
use std::os::unix::net::UnixStream;
28-
#[cfg(all(unix, feature = "tokio"))]
29-
use tokio::net::UnixStream;
30-
#[cfg(windows)]
31-
use uds_windows::UnixStream;
32-
3326
use std::{
3427
fmt::{Display, Formatter},
3528
str::from_utf8_unchecked,
3629
};
3730

3831
mod unix;
39-
pub use unix::{Unix, UnixSocket};
32+
pub use unix::{Unix, UnixSocket, UnixStream};
4033
mod tcp;
4134
pub use tcp::{Tcp, TcpTransportFamily};
4235
#[cfg(windows)]
@@ -103,62 +96,7 @@ impl Transport {
10396
#[cfg_attr(any(unix, windows), async_recursion::async_recursion)]
10497
pub(super) async fn connect(self) -> Result<Stream> {
10598
match self {
106-
Transport::Unix(unix) => {
107-
// This is a `path` because neither uds_windows, tokio, nor async_io provide
108-
// the SocketAddrExt functions.
109-
let addr = match unix.take_path() {
110-
UnixSocket::File(path) => path,
111-
#[cfg(target_os = "linux")]
112-
UnixSocket::Abstract(name) => {
113-
use std::{ffi::OsString, os::unix::ffi::OsStringExt, path::PathBuf};
114-
let mut v = name.into_vec();
115-
v.insert(0, 0);
116-
PathBuf::from(OsString::from_vec(v))
117-
}
118-
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
119-
// You can't connect to a unix:dir.
120-
return Err(Error::Unsupported);
121-
}
122-
};
123-
124-
#[cfg(not(feature = "tokio"))]
125-
{
126-
#[cfg(unix)]
127-
let stream = Async::<UnixStream>::connect(addr).await;
128-
129-
#[cfg(not(unix))]
130-
let stream = {
131-
let stream = crate::Task::spawn_blocking(
132-
move || -> Result<_> {
133-
let stream = UnixStream::connect(addr)?;
134-
stream.set_nonblocking(true)?;
135-
136-
Ok(stream)
137-
},
138-
"unix stream connection",
139-
)
140-
.await??;
141-
Async::new(stream)
142-
};
143-
144-
stream
145-
.map(Stream::Unix)
146-
.map_err(|e| Error::InputOutput(e.into()))
147-
}
148-
149-
#[cfg(feature = "tokio")]
150-
{
151-
#[cfg(unix)]
152-
{
153-
UnixStream::connect(addr)
154-
.await
155-
.map_err(|e| Error::InputOutput(e.into()))
156-
}
157-
158-
#[cfg(not(unix))]
159-
Err(Error::Unsupported)
160-
}
161-
}
99+
Transport::Unix(unix) => unix.connect().await.map(Stream::Unix),
162100
#[cfg(unix)]
163101
Transport::Unixexec(unixexec) => unixexec.connect().await.map(Stream::Unixexec),
164102
#[cfg(all(feature = "vsock", not(feature = "tokio")))]

zbus/src/address/transport/unix.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::{Error, Result};
2+
13
#[cfg(target_os = "linux")]
24
use std::ffi::OsString;
35
use std::{
@@ -9,6 +11,16 @@ use std::{
911
#[cfg(unix)]
1012
use super::encode_percents;
1113

14+
#[cfg(not(feature = "tokio"))]
15+
use async_io::Async;
16+
17+
#[cfg(all(unix, not(feature = "tokio")))]
18+
pub use std::os::unix::net::UnixStream;
19+
#[cfg(all(unix, feature = "tokio"))]
20+
pub use tokio::net::UnixStream;
21+
#[cfg(windows)]
22+
pub use uds_windows::UnixStream;
23+
1224
/// A Unix domain socket transport in a D-Bus address.
1325
#[derive(Clone, Debug, PartialEq, Eq)]
1426
pub struct Unix {
@@ -31,6 +43,25 @@ impl Unix {
3143
self.path
3244
}
3345

46+
fn take_addr(self) -> Result<PathBuf> {
47+
// This is a `path` because neither uds_windows, tokio, nor async_io provide
48+
// the SocketAddrExt functions.
49+
match self.take_path() {
50+
UnixSocket::File(path) => Ok(path),
51+
#[cfg(target_os = "linux")]
52+
UnixSocket::Abstract(name) => {
53+
use std::{ffi::OsString, os::unix::ffi::OsStringExt, path::PathBuf};
54+
let mut v = name.into_vec();
55+
v.insert(0, 0);
56+
Ok(PathBuf::from(OsString::from_vec(v)))
57+
}
58+
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
59+
// You can't connect to a unix:dir.
60+
Err(Error::Unsupported)
61+
}
62+
}
63+
}
64+
3465
pub(super) fn from_options(opts: std::collections::HashMap<&str, &str>) -> crate::Result<Self> {
3566
let path = opts.get("path");
3667
let abs = opts.get("abstract");
@@ -55,6 +86,45 @@ impl Unix {
5586

5687
Ok(Self::new(path))
5788
}
89+
90+
#[cfg(not(feature = "tokio"))]
91+
pub(super) async fn connect(self) -> Result<Async<UnixStream>> {
92+
let addr = self.take_addr()?;
93+
94+
#[cfg(unix)]
95+
let stream = Async::<UnixStream>::connect(addr).await;
96+
97+
#[cfg(not(unix))]
98+
let stream = {
99+
let stream = crate::Task::spawn_blocking(
100+
move || -> Result<_> {
101+
let stream = UnixStream::connect(addr)?;
102+
stream.set_nonblocking(true)?;
103+
104+
Ok(stream)
105+
},
106+
"unix stream connection",
107+
)
108+
.await??;
109+
Async::new(stream)
110+
};
111+
112+
stream.map_err(|e| Error::InputOutput(e.into()))
113+
}
114+
115+
#[cfg(feature = "tokio")]
116+
pub(super) async fn connect(self) -> Result<UnixStream> {
117+
#[cfg(unix)]
118+
{
119+
let addr = self.take_addr()?;
120+
UnixStream::connect(addr)
121+
.await
122+
.map_err(|e| Error::InputOutput(e.into()))
123+
}
124+
125+
#[cfg(not(unix))]
126+
Err(Error::Unsupported)
127+
}
58128
}
59129

60130
impl Display for Unix {

0 commit comments

Comments
 (0)