-
-
Notifications
You must be signed in to change notification settings - Fork 160
🧵 zb: Use async versions of UnixStream when possible #1798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| #[cfg(any(unix, not(feature = "tokio")))] | ||
| use crate::{Error, Result}; | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| use std::ffi::OsString; | ||
| use std::{ | ||
|
|
@@ -9,6 +12,14 @@ use std::{ | |
| #[cfg(unix)] | ||
| use super::encode_percents; | ||
|
|
||
| #[cfg(not(feature = "tokio"))] | ||
| use async_io::Async; | ||
|
|
||
| #[cfg(all(unix, not(feature = "tokio")))] | ||
| use std::os::unix::net::UnixStream; | ||
| #[cfg(all(windows, not(feature = "tokio")))] | ||
| use uds_windows::UnixStream; | ||
|
|
||
| /// A Unix domain socket transport in a D-Bus address. | ||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub struct Unix { | ||
|
|
@@ -55,6 +66,63 @@ impl Unix { | |
|
|
||
| Ok(Self::new(path)) | ||
| } | ||
|
|
||
| #[cfg(not(feature = "tokio"))] | ||
| pub(super) async fn connect(self) -> Result<Async<UnixStream>> { | ||
| let addr = self.take_addr()?; | ||
|
|
||
| #[cfg(unix)] | ||
| let stream = Async::<UnixStream>::connect(addr).await; | ||
|
|
||
| #[cfg(not(unix))] | ||
| let stream = Async::new(self.get_stream(addr).await?); | ||
|
|
||
| stream.map_err(|e| Error::InputOutput(e.into())) | ||
| } | ||
|
|
||
| #[cfg(all(unix, feature = "tokio"))] | ||
| pub(super) async fn connect(self) -> Result<tokio::net::UnixStream> { | ||
| let addr = self.take_addr()?; | ||
| tokio::net::UnixStream::connect(addr) | ||
| .await | ||
| .map_err(|e| Error::InputOutput(e.into())) | ||
| } | ||
|
|
||
| #[cfg(not(unix))] | ||
| async fn get_stream(self, addr: PathBuf) -> Result<UnixStream> { | ||
| crate::Task::spawn_blocking( | ||
| move || -> Result<_> { | ||
| let stream = UnixStream::connect(addr)?; | ||
| stream.set_nonblocking(true)?; | ||
|
|
||
| Ok(stream) | ||
| }, | ||
| "unix stream connection", | ||
| ) | ||
| .await? | ||
| } | ||
|
|
||
| #[cfg(any(unix, not(feature = "tokio")))] | ||
| fn take_addr(self) -> Result<PathBuf> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're renaming a method in the last commit that you created in the previous one and the commit still seems more intrusive than it should be IMO. This commit should mostly just be replacing spawn_blocking use for |
||
| // This is a `path` because neither uds_windows, tokio, nor async_io provide | ||
| // the SocketAddrExt functions. | ||
| match self.take_path() { | ||
| UnixSocket::File(path) => Ok(path), | ||
| #[cfg(target_os = "linux")] | ||
| UnixSocket::Abstract(name) => { | ||
| use std::os::unix::ffi::OsStringExt; | ||
|
|
||
| let mut v = name.into_vec(); | ||
| v.insert(0, 0); | ||
|
|
||
| Ok(PathBuf::from(OsString::from_vec(v))) | ||
|
germaniuss marked this conversation as resolved.
|
||
| } | ||
| UnixSocket::Dir(_) | UnixSocket::TmpDir(_) => { | ||
| // You can't connect to a unix:dir. | ||
| Err(Error::Unsupported) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Display for Unix { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.