Skip to content

Commit

Permalink
feat: support more addr features for non-linux unix
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Jul 18, 2023
1 parent ca7877a commit 4a3963d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions monoio/src/driver/legacy/scheduled_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ impl ScheduledIo {
self.readiness = self.readiness - ready;
}

#[allow(clippy::needless_pass_by_ref_mut)]
pub(crate) fn poll_readiness(
&mut self,
cx: &mut Context<'_>,
Expand Down
1 change: 1 addition & 0 deletions monoio/src/driver/uring/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<'a> Ref<'a, Lifecycle> {
}
}

#[allow(clippy::needless_pass_by_ref_mut)]
pub(crate) fn poll_op(mut self, cx: &mut Context<'_>) -> Poll<CompletionMeta> {
let ref_mut = &mut *self;
match ref_mut {
Expand Down
7 changes: 3 additions & 4 deletions monoio/src/net/unix/socket_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ impl SocketAddr {
let len = self.socklen as usize - offset;
let path = unsafe { &*(&self.sockaddr.sun_path as *const [libc::c_char] as *const [u8]) };

if len == 0 {
// macOS seems to return a len of 16 and a zeroed sun_path for unnamed addresses
if len == 0 || (cfg!(not(any(target_os = "linux", target_os = "android"))) && path[0] == 0)
{
AddressKind::Unnamed
} else if self.sockaddr.sun_path[0] == 0 {
AddressKind::Abstract(&path[1..len])
Expand Down Expand Up @@ -94,7 +96,6 @@ impl SocketAddr {
/// Documentation reflected in [`SocketAddr`]
///
/// [`SocketAddr`]: std::os::unix::net::SocketAddr
#[cfg(target_os = "linux")]
#[inline]
pub fn is_unnamed(&self) -> bool {
matches!(self.address(), AddressKind::Unnamed)
Expand All @@ -105,7 +106,6 @@ impl SocketAddr {
/// Documentation reflected in [`SocketAddr`]
///
/// [`SocketAddr`]: std::os::unix::net::SocketAddr
#[cfg(target_os = "linux")]
#[inline]
pub fn as_pathname(&self) -> Option<&Path> {
if let AddressKind::Pathname(path) = self.address() {
Expand All @@ -119,7 +119,6 @@ impl SocketAddr {
/// without the leading null byte.
// Link to std::os::unix::net::SocketAddr pending
// https://github.com/rust-lang/rust/issues/85410.
#[cfg(target_os = "linux")]
#[inline]
pub fn as_abstract_namespace(&self) -> Option<&[u8]> {
if let AddressKind::Abstract(path) = self.address() {
Expand Down
1 change: 1 addition & 0 deletions monoio/src/time/driver/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ impl TimerEntry {
}
}

#[allow(clippy::needless_pass_by_ref_mut)]
pub(crate) fn poll_elapsed(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
21 changes: 19 additions & 2 deletions monoio/tests/unix_datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ async fn accept_send_recv() -> std::io::Result<()> {
dgram2.send(b"hello").await.0.unwrap();
let (_res, buf) = dgram1.recv_from(vec![0; 100]).await;
assert_eq!(buf, b"hello");

#[cfg(target_os = "linux")]
assert!(_res.unwrap().1.is_unnamed());

let dgram3 = UnixDatagram::unbound()?;
Expand All @@ -25,3 +23,22 @@ async fn accept_send_recv() -> std::io::Result<()> {
assert_eq!(res.unwrap(), 6);
Ok(())
}

#[monoio::test_all]
async fn addr_type() -> std::io::Result<()> {
let dir = tempfile::Builder::new()
.prefix("monoio-unix-datagram-tests")
.tempdir()
.unwrap();
let sock_path1 = dir.path().join("dgram_addr1.sock");
let sock_path2 = dir.path().join("dgram_addr2.sock");

let dgram1 = UnixDatagram::bind(&sock_path1)?;
let dgram2 = UnixDatagram::bind(&sock_path2)?;

dgram1.send_to(b"hello", sock_path2).await.0.unwrap();
let (_res, buf) = dgram2.recv_from(vec![0; 100]).await;
assert_eq!(buf, b"hello");
assert_eq!(_res.unwrap().1.as_pathname(), Some(sock_path1.as_path()));
Ok(())
}

0 comments on commit 4a3963d

Please sign in to comment.