Skip to content

Commit b3f0a61

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 b3f0a61

2 files changed

Lines changed: 82 additions & 57 deletions

File tree

zbus/src/address/transport/mod.rs

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ 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};
15+
#[cfg(all(unix, not(feature = "tokio")))]
16+
use std::os::unix::net::UnixStream;
1717
#[cfg(feature = "tokio")]
1818
use tokio::net::TcpStream;
1919
#[cfg(feature = "tokio-vsock")]
2020
use tokio_vsock::VsockStream;
21-
#[cfg(windows)]
21+
#[cfg(all(windows, not(feature = "tokio")))]
2222
use uds_windows::UnixStream;
2323
#[cfg(all(feature = "vsock", not(feature = "tokio")))]
2424
use vsock::VsockStream;
@@ -55,8 +55,6 @@ pub use ibus::Ibus;
5555
#[path = "vsock.rs"]
5656
// Gotta rename to avoid name conflict with the `vsock` crate.
5757
mod vsock_transport;
58-
#[cfg(target_os = "linux")]
59-
use std::os::linux::net::SocketAddrExt;
6058
#[cfg(any(
6159
all(feature = "vsock", not(feature = "tokio")),
6260
feature = "tokio-vsock"
@@ -102,59 +100,11 @@ impl Transport {
102100
#[cfg_attr(any(unix, windows), async_recursion::async_recursion)]
103101
pub(super) async fn connect(self) -> Result<Stream> {
104102
match self {
105-
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-
}
103+
#[cfg(all(not(unix), feature = "tokio"))]
104+
Transport::Unix(_) => Err(Error::Unsupported),
105+
#[cfg(any(unix, not(feature = "tokio")))]
106+
Transport::Unix(unix) => unix.connect().await.map(Stream::Unix),
141107

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-
}
157-
}
158108
#[cfg(unix)]
159109
Transport::Unixexec(unixexec) => unixexec.connect().await.map(Stream::Unixexec),
160110
#[cfg(all(feature = "vsock", not(feature = "tokio")))]

zbus/src/address/transport/unix.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[cfg(any(unix, not(feature = "tokio")))]
2+
use crate::{Error, Result};
3+
14
#[cfg(target_os = "linux")]
25
use std::ffi::OsString;
36
use std::{
@@ -9,6 +12,16 @@ 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, UnixStream};
22+
#[cfg(all(windows, not(feature = "tokio")))]
23+
use uds_windows::UnixStream;
24+
1225
/// A Unix domain socket transport in a D-Bus address.
1326
#[derive(Clone, Debug, PartialEq, Eq)]
1427
pub struct Unix {
@@ -55,6 +68,68 @@ impl Unix {
5568

5669
Ok(Self::new(path))
5770
}
71+
72+
#[cfg(not(feature = "tokio"))]
73+
pub(super) async fn connect(self) -> Result<Async<UnixStream>> {
74+
let stream = self.get_stream().await?;
75+
Async::new(stream).map_err(|e| Error::InputOutput(e.into()))
76+
}
77+
78+
#[cfg(all(unix, feature = "tokio"))]
79+
pub(super) async fn connect(self) -> Result<tokio::net::UnixStream> {
80+
let stream = self.get_stream().await?;
81+
tokio::net::UnixStream::from_std(stream).map_err(|e| Error::InputOutput(e.into()))
82+
}
83+
84+
#[cfg(any(unix, not(feature = "tokio")))]
85+
async fn get_stream(self) -> Result<UnixStream> {
86+
#[cfg(unix)]
87+
let addr = self.take_socket_addr()?;
88+
#[cfg(windows)]
89+
let addr = self.take_path_addr()?;
90+
91+
crate::Task::spawn_blocking(
92+
move || -> Result<_> {
93+
#[cfg(unix)]
94+
let stream = UnixStream::connect_addr(&addr)?;
95+
#[cfg(windows)]
96+
let stream = UnixStream::connect(addr)?;
97+
stream.set_nonblocking(true)?;
98+
99+
Ok(stream)
100+
},
101+
"unix stream connection",
102+
)
103+
.await?
104+
}
105+
106+
#[cfg(all(windows, not(feature = "tokio")))]
107+
fn take_path_addr(self) -> Result<PathBuf> {
108+
// This is a `path` in case of Windows until uds_windows provides the needed API:
109+
// https://github.com/haraldh/rust_uds_windows/issues/14
110+
match self.take_path() {
111+
UnixSocket::File(path) => Ok(path),
112+
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
113+
// You can't connect to a unix:dir.
114+
Err(Error::Unsupported)
115+
}
116+
}
117+
}
118+
119+
#[cfg(unix)]
120+
fn take_socket_addr(self) -> Result<SocketAddr> {
121+
match self.take_path() {
122+
UnixSocket::File(path) => Ok(SocketAddr::from_pathname(path)?),
123+
#[cfg(target_os = "linux")]
124+
UnixSocket::Abstract(name) => {
125+
Ok(SocketAddr::from_abstract_name(name.as_encoded_bytes())?)
126+
}
127+
UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => {
128+
// You can't connect to a unix:dir.
129+
Err(Error::Unsupported)
130+
}
131+
}
132+
}
58133
}
59134

60135
impl Display for Unix {

0 commit comments

Comments
 (0)