Skip to content

Commit e46f3f6

Browse files
committed
unix: Implement Eq, Hash, PartialEq for sockaddr_* types
1 parent 8a48885 commit e46f3f6

File tree

8 files changed

+63
-0
lines changed

8 files changed

+63
-0
lines changed

src/dox.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod imp {
66
pub use core::clone::Clone;
77
pub use core::marker::Copy;
88
pub use core::mem;
9+
pub use core::hash::{Hash, Hasher};
910
}
1011

1112
#[cfg(dox)]

src/unix/bsd/apple/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ s! {
173173
pub dli_saddr: *mut ::c_void,
174174
}
175175

176+
#[derive(Eq, Hash, PartialEq)]
176177
pub struct sockaddr_in {
177178
pub sin_len: u8,
178179
pub sin_family: ::sa_family_t,

src/unix/bsd/freebsdlike/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ s! {
113113
pub dli_saddr: *mut ::c_void,
114114
}
115115

116+
#[derive(Eq, Hash, PartialEq)]
116117
pub struct sockaddr_in {
117118
pub sin_len: u8,
118119
pub sin_family: ::sa_family_t,

src/unix/bsd/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use dox::{Hash, Hasher};
12
use dox::mem;
23

34
pub type c_char = i8;
@@ -11,12 +12,14 @@ pub type pthread_t = ::uintptr_t;
1112
pub type nfds_t = ::c_uint;
1213

1314
s! {
15+
#[derive(Eq, Hash, PartialEq)]
1416
pub struct sockaddr {
1517
pub sa_len: u8,
1618
pub sa_family: sa_family_t,
1719
pub sa_data: [::c_char; 14],
1820
}
1921

22+
#[derive(Eq, Hash, PartialEq)]
2023
pub struct sockaddr_in6 {
2124
pub sin6_len: u8,
2225
pub sin6_family: sa_family_t,
@@ -107,6 +110,21 @@ s! {
107110
}
108111
}
109112

113+
impl PartialEq for sockaddr_un {
114+
fn eq(&self, other: &sockaddr_un) -> bool {
115+
self.sun_family == other.sun_family && &self.sun_path[..] == &other.sun_path[..]
116+
}
117+
}
118+
119+
impl Eq for sockaddr_un { }
120+
121+
impl Hash for sockaddr_un {
122+
fn hash<H>(&self, state: &mut H) where H: Hasher {
123+
self.sun_family.hash(state);
124+
&self.sun_path[..].hash(state);
125+
}
126+
}
127+
110128
pub const FIOCLEX: ::c_ulong = 0x20006601;
111129
pub const FIONBIO: ::c_ulong = 0x8004667e;
112130

src/unix/bsd/openbsdlike/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ s! {
2424
pub ss_flags: ::c_int,
2525
}
2626

27+
#[derive(Eq, Hash, PartialEq)]
2728
pub struct sockaddr_in {
2829
pub sin_len: u8,
2930
pub sin_family: ::sa_family_t,

src/unix/notbsd/linux/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ s! {
125125
__f_spare: [::c_int; 6],
126126
}
127127

128+
#[derive(Eq, Hash, PartialEq)]
128129
pub struct sockaddr_nl {
129130
pub nl_family: ::sa_family_t,
130131
nl_pad: ::c_ushort,

src/unix/notbsd/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use dox::{Hash, Hasher};
12
use dox::mem;
23

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

1112
s! {
13+
#[derive(Eq, Hash, PartialEq)]
1214
pub struct sockaddr {
1315
pub sa_family: sa_family_t,
1416
pub sa_data: [::c_char; 14],
1517
}
1618

19+
#[derive(Eq, Hash, PartialEq)]
1720
pub struct sockaddr_in {
1821
pub sin_family: sa_family_t,
1922
pub sin_port: ::in_port_t,
2023
pub sin_addr: ::in_addr,
2124
pub sin_zero: [u8; 8],
2225
}
2326

27+
#[derive(Eq, Hash, PartialEq)]
2428
pub struct sockaddr_in6 {
2529
pub sin6_family: sa_family_t,
2630
pub sin6_port: ::in_port_t,
@@ -61,6 +65,7 @@ s! {
6165
pub ai_next: *mut addrinfo,
6266
}
6367

68+
#[derive(Eq, Hash, PartialEq)]
6469
pub struct sockaddr_ll {
6570
pub sll_family: ::c_ushort,
6671
pub sll_protocol: ::c_ushort,
@@ -125,6 +130,21 @@ s! {
125130
}
126131
}
127132

133+
impl PartialEq for sockaddr_un {
134+
fn eq(&self, other: &sockaddr_un) -> bool {
135+
self.sun_family == other.sun_family && &self.sun_path[..] == &other.sun_path[..]
136+
}
137+
}
138+
139+
impl Eq for sockaddr_un { }
140+
141+
impl Hash for sockaddr_un {
142+
fn hash<H>(&self, state: &mut H) where H: Hasher {
143+
self.sun_family.hash(state);
144+
&self.sun_path[..].hash(state);
145+
}
146+
}
147+
128148
// intentionally not public, only used for fd_set
129149
#[cfg(target_pointer_width = "32")]
130150
const ULONG_SIZE: usize = 32;

src/unix/sunos/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use dox::{Hash, Hasher};
2+
13
pub type c_char = i8;
24
pub type c_long = i64;
35
pub type c_ulong = u64;
@@ -34,18 +36,21 @@ pub type fflags_t = u32;
3436
pub enum timezone {}
3537

3638
s! {
39+
#[derive(Eq, Hash, PartialEq)]
3740
pub struct sockaddr {
3841
pub sa_family: sa_family_t,
3942
pub sa_data: [::c_char; 14],
4043
}
4144

45+
#[derive(Eq, Hash, PartialEq)]
4246
pub struct sockaddr_in {
4347
pub sin_family: sa_family_t,
4448
pub sin_port: ::in_port_t,
4549
pub sin_addr: ::in_addr,
4650
pub sin_zero: [::c_char; 8]
4751
}
4852

53+
#[derive(Eq, Hash, PartialEq)]
4954
pub struct sockaddr_in6 {
5055
pub sin6_family: sa_family_t,
5156
pub sin6_port: ::in_port_t,
@@ -273,6 +278,21 @@ s! {
273278
}
274279
}
275280

281+
impl PartialEq for sockaddr_un {
282+
fn eq(&self, other: &sockaddr_un) -> bool {
283+
self.sun_family == other.sun_family && &self.sun_path[..] == &other.sun_path[..]
284+
}
285+
}
286+
287+
impl Eq for sockaddr_un { }
288+
289+
impl Hash for sockaddr_un {
290+
fn hash<H>(&self, state: &mut H) where H: Hasher {
291+
self.sun_family.hash(state);
292+
&self.sun_path[..].hash(state);
293+
}
294+
}
295+
276296
pub const SA_ONSTACK: ::c_int = 0x00000001;
277297
pub const SA_RESETHAND: ::c_int = 0x00000002;
278298
pub const SA_RESTART: ::c_int = 0x00000004;

0 commit comments

Comments
 (0)