Skip to content
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

unix: Implement Eq, Hash, PartialEq for sockaddr_* types #159

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/dox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod imp {
pub use core::clone::Clone;
pub use core::marker::Copy;
pub use core::mem;
pub use core::hash::{Hash, Hasher};
}

#[cfg(dox)]
Expand Down
1 change: 1 addition & 0 deletions src/unix/bsd/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ s! {
pub dli_saddr: *mut ::c_void,
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in {
pub sin_len: u8,
pub sin_family: ::sa_family_t,
Expand Down
1 change: 1 addition & 0 deletions src/unix/bsd/freebsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ s! {
pub dli_saddr: *mut ::c_void,
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in {
pub sin_len: u8,
pub sin_family: ::sa_family_t,
Expand Down
18 changes: 18 additions & 0 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use dox::{Hash, Hasher};
use dox::mem;

pub type c_char = i8;
Expand All @@ -11,12 +12,14 @@ pub type pthread_t = ::uintptr_t;
pub type nfds_t = ::c_uint;

s! {
#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr {
pub sa_len: u8,
pub sa_family: sa_family_t,
pub sa_data: [::c_char; 14],
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in6 {
pub sin6_len: u8,
pub sin6_family: sa_family_t,
Expand Down Expand Up @@ -107,6 +110,21 @@ s! {
}
}

impl PartialEq for sockaddr_un {
fn eq(&self, other: &sockaddr_un) -> bool {
self.sun_family == other.sun_family && &self.sun_path[..] == &other.sun_path[..]
}
}

impl Eq for sockaddr_un { }

impl Hash for sockaddr_un {
fn hash<H>(&self, state: &mut H) where H: Hasher {
self.sun_family.hash(state);
&self.sun_path[..].hash(state);
}
}

pub const FIOCLEX: ::c_ulong = 0x20006601;
pub const FIONBIO: ::c_ulong = 0x8004667e;

Expand Down
1 change: 1 addition & 0 deletions src/unix/bsd/openbsdlike/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ s! {
pub ss_flags: ::c_int,
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in {
pub sin_len: u8,
pub sin_family: ::sa_family_t,
Expand Down
2 changes: 2 additions & 0 deletions src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ s! {
}

#[cfg_attr(target_os = "netbsd", repr(packed))]
#[derive(Eq, Hash, PartialEq)]
pub struct in_addr {
pub s_addr: in_addr_t,
}

#[derive(Eq, Hash, PartialEq)]
pub struct in6_addr {
pub s6_addr: [u8; 16],
__align: [u32; 0],
Expand Down
1 change: 1 addition & 0 deletions src/unix/notbsd/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ s! {
__f_spare: [::c_int; 6],
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_nl {
pub nl_family: ::sa_family_t,
nl_pad: ::c_ushort,
Expand Down
20 changes: 20 additions & 0 deletions src/unix/notbsd/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use dox::{Hash, Hasher};
use dox::mem;

pub type rlim_t = c_ulong;
Expand All @@ -9,18 +10,21 @@ pub type tcflag_t = ::c_uint;
pub enum timezone {}

s! {
#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr {
pub sa_family: sa_family_t,
pub sa_data: [::c_char; 14],
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in {
pub sin_family: sa_family_t,
pub sin_port: ::in_port_t,
pub sin_addr: ::in_addr,
pub sin_zero: [u8; 8],
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in6 {
pub sin6_family: sa_family_t,
pub sin6_port: ::in_port_t,
Expand Down Expand Up @@ -61,6 +65,7 @@ s! {
pub ai_next: *mut addrinfo,
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_ll {
pub sll_family: ::c_ushort,
pub sll_protocol: ::c_ushort,
Expand Down Expand Up @@ -125,6 +130,21 @@ s! {
}
}

impl PartialEq for sockaddr_un {
fn eq(&self, other: &sockaddr_un) -> bool {
self.sun_family == other.sun_family && &self.sun_path[..] == &other.sun_path[..]
}
}

impl Eq for sockaddr_un { }

impl Hash for sockaddr_un {
fn hash<H>(&self, state: &mut H) where H: Hasher {
self.sun_family.hash(state);
&self.sun_path[..].hash(state);
}
}

// intentionally not public, only used for fd_set
#[cfg(target_pointer_width = "32")]
const ULONG_SIZE: usize = 32;
Expand Down
20 changes: 20 additions & 0 deletions src/unix/sunos/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use dox::{Hash, Hasher};

pub type c_char = i8;
pub type c_long = i64;
pub type c_ulong = u64;
Expand Down Expand Up @@ -34,18 +36,21 @@ pub type fflags_t = u32;
pub enum timezone {}

s! {
#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr {
pub sa_family: sa_family_t,
pub sa_data: [::c_char; 14],
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in {
pub sin_family: sa_family_t,
pub sin_port: ::in_port_t,
pub sin_addr: ::in_addr,
pub sin_zero: [::c_char; 8]
}

#[derive(Eq, Hash, PartialEq)]
pub struct sockaddr_in6 {
pub sin6_family: sa_family_t,
pub sin6_port: ::in_port_t,
Expand Down Expand Up @@ -273,6 +278,21 @@ s! {
}
}

impl PartialEq for sockaddr_un {
fn eq(&self, other: &sockaddr_un) -> bool {
self.sun_family == other.sun_family && &self.sun_path[..] == &other.sun_path[..]
}
}

impl Eq for sockaddr_un { }

impl Hash for sockaddr_un {
fn hash<H>(&self, state: &mut H) where H: Hasher {
self.sun_family.hash(state);
&self.sun_path[..].hash(state);
}
}

pub const SA_ONSTACK: ::c_int = 0x00000001;
pub const SA_RESETHAND: ::c_int = 0x00000002;
pub const SA_RESTART: ::c_int = 0x00000004;
Expand Down