From 558bbbe4378ce083547e01d6cfc6f130064b9450 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Mon, 10 Apr 2023 15:42:06 +0200 Subject: [PATCH 1/3] Move SockAddr::unix down This shouldn't be the first item in the documentation. --- src/sockaddr.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/sockaddr.rs b/src/sockaddr.rs index 5dbecaa0..6d63b81a 100644 --- a/src/sockaddr.rs +++ b/src/sockaddr.rs @@ -25,16 +25,6 @@ pub struct SockAddr { #[allow(clippy::len_without_is_empty)] impl SockAddr { - /// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path. - /// - /// Returns an error if the path is longer than `SUN_LEN`. - pub fn unix

(path: P) -> io::Result - where - P: AsRef, - { - crate::sys::unix_sockaddr(path.as_ref()) - } - /// Create a `SockAddr` from the underlying storage and its length. /// /// # Safety @@ -153,6 +143,16 @@ impl SockAddr { }) } + /// Constructs a `SockAddr` with the family `AF_UNIX` and the provided path. + /// + /// Returns an error if the path is longer than `SUN_LEN`. + pub fn unix

(path: P) -> io::Result + where + P: AsRef, + { + crate::sys::unix_sockaddr(path.as_ref()) + } + /// Returns this address's family. pub const fn family(&self) -> sa_family_t { self.storage.ss_family From c1525ec664abf1bbe8bc88941b786b161e49e0de Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Mon, 10 Apr 2023 15:43:23 +0200 Subject: [PATCH 2/3] Add SockAddr::set_length --- src/sockaddr.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/sockaddr.rs b/src/sockaddr.rs index 6d63b81a..029da6ad 100644 --- a/src/sockaddr.rs +++ b/src/sockaddr.rs @@ -153,6 +153,16 @@ impl SockAddr { crate::sys::unix_sockaddr(path.as_ref()) } + /// Set the length of the address. + /// + /// # Safety + /// + /// Caller must ensure that the address up to `length` bytes are properly + /// initialised. + pub unsafe fn set_length(&mut self, length: socklen_t) { + self.len = length; + } + /// Returns this address's family. pub const fn family(&self) -> sa_family_t { self.storage.ss_family From 803ed2ee8d83213018225ceb9a37814bf8bc1551 Mon Sep 17 00:00:00 2001 From: Thomas de Zeeuw Date: Mon, 10 Apr 2023 15:48:33 +0200 Subject: [PATCH 3/3] Cleanup PartialEq and Hash impls for SockAddr --- src/sockaddr.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/sockaddr.rs b/src/sockaddr.rs index 029da6ad..53342270 100644 --- a/src/sockaddr.rs +++ b/src/sockaddr.rs @@ -260,6 +260,14 @@ impl SockAddr { _ => None, } } + + /// Returns the initialised storage bytes. + fn as_bytes(&self) -> &[u8] { + // SAFETY: `self.storage` is a C struct which can always be treated a + // slice of bytes. Futhermore we ensure we don't read any unitialised + // bytes by using `self.len`. + unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), self.len as usize) } + } } impl From for SockAddr { @@ -339,11 +347,7 @@ impl fmt::Debug for SockAddr { impl PartialEq for SockAddr { fn eq(&self, other: &Self) -> bool { - unsafe { - let these_bytes: &[u8] = any_as_u8_slice(&self.storage, self.len as usize); - let those_bytes: &[u8] = any_as_u8_slice(&other.storage, other.len as usize); - these_bytes == those_bytes - } + self.as_bytes() == other.as_bytes() } } @@ -351,17 +355,10 @@ impl Eq for SockAddr {} impl Hash for SockAddr { fn hash(&self, state: &mut H) { - unsafe { - let these_bytes: &[u8] = any_as_u8_slice(&self.storage, self.len as usize); - these_bytes.hash(state); - } + self.as_bytes().hash(state); } } -unsafe fn any_as_u8_slice(p: &T, size: usize) -> &[u8] { - ::std::slice::from_raw_parts((p as *const T) as *const u8, size) -} - #[cfg(test)] mod tests { use super::*;