Skip to content

Commit

Permalink
unix: Implement Eq, Hash, PartialEq for sockaddr_* types
Browse files Browse the repository at this point in the history
  • Loading branch information
kamalmarhubi committed Jan 28, 2016
1 parent 8a48885 commit 2fadf4c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 0 deletions.
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
17 changes: 17 additions & 0 deletions src/unix/bsd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,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 +109,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
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

0 comments on commit 2fadf4c

Please sign in to comment.