Skip to content

Commit cd1e16d

Browse files
committed
Implement Hash for all types
1 parent 79ae121 commit cd1e16d

File tree

17 files changed

+394
-3
lines changed

17 files changed

+394
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ libc = { version = "0.2", features = ["align"] }
4545
```
4646

4747
All structs implemented by the libc crate have the `Copy` and `Clone` traits
48-
implemented for them. The additional traits of `Debug, `Eq`, and `PartialEq`
49-
can be enabled with the *extra_traits* feature (requires Rust 1.25 or newer):
48+
implemented for them. The additional traits of `Debug, `Eq`, `Hash`, and
49+
`PartialEq` can be enabled with the *extra_traits* feature (requires Rust 1.25
50+
or newer):
5051

5152
```toml
5253
[dependencies]

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ macro_rules! s {
3939
__item! {
4040
#[repr(C)]
4141
$(#[$attr])*
42-
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, PartialEq))]
42+
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
4343
pub $t $i { $($field)* }
4444
}
4545
impl ::dox::Copy for $i {}

src/unix/bsd/apple/b32.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ impl std::fmt::Debug for pthread_attr_t {
7373
.finish()
7474
}
7575
}
76+
#[cfg(feature = "extra_traits")]
77+
impl std::hash::Hash for pthread_attr_t {
78+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
79+
self.__sig.hash(state);
80+
self.__opaque.hash(state);
81+
}
82+
}
7683

7784
pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
7885
pub const __PTHREAD_COND_SIZE__: usize = 24;

src/unix/bsd/apple/b64.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ impl std::fmt::Debug for pthread_attr_t {
7878
.finish()
7979
}
8080
}
81+
#[cfg(feature = "extra_traits")]
82+
impl std::hash::Hash for pthread_attr_t {
83+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
84+
self.__sig.hash(state);
85+
self.__opaque.hash(state);
86+
}
87+
}
8188

8289
pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
8390
pub const __PTHREAD_COND_SIZE__: usize = 40;

src/unix/bsd/apple/mod.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,12 @@ impl std::fmt::Debug for semun {
608608
}
609609
}
610610
#[cfg(feature = "extra_traits")]
611+
impl std::hash::Hash for semun {
612+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
613+
unsafe { self.val.hash(state) };
614+
}
615+
}
616+
#[cfg(feature = "extra_traits")]
611617
impl PartialEq for proc_threadinfo {
612618
fn eq(&self, other: &proc_threadinfo) -> bool {
613619
self.pth_user_time == other.pth_user_time
@@ -648,6 +654,22 @@ impl std::fmt::Debug for proc_threadinfo {
648654
}
649655
}
650656
#[cfg(feature = "extra_traits")]
657+
impl std::hash::Hash for proc_threadinfo {
658+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
659+
self.pth_user_time.hash(state);
660+
self.pth_system_time.hash(state);
661+
self.pth_cpu_usage.hash(state);
662+
self.pth_policy.hash(state);
663+
self.pth_run_state.hash(state);
664+
self.pth_flags.hash(state);
665+
self.pth_sleep_time.hash(state);
666+
self.pth_curpri.hash(state);
667+
self.pth_priority.hash(state);
668+
self.pth_maxpriority.hash(state);
669+
self.pth_name.hash(state);
670+
}
671+
}
672+
#[cfg(feature = "extra_traits")]
651673
impl PartialEq for statfs {
652674
fn eq(&self, other: &statfs) -> bool {
653675
self.f_bsize == other.f_bsize
@@ -702,6 +724,27 @@ impl std::fmt::Debug for statfs {
702724
}
703725
}
704726
#[cfg(feature = "extra_traits")]
727+
impl std::hash::Hash for statfs {
728+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
729+
self.f_bsize.hash(state);
730+
self.f_iosize.hash(state);
731+
self.f_blocks.hash(state);
732+
self.f_bfree.hash(state);
733+
self.f_bavail.hash(state);
734+
self.f_files.hash(state);
735+
self.f_ffree.hash(state);
736+
self.f_fsid.hash(state);
737+
self.f_owner.hash(state);
738+
self.f_flags.hash(state);
739+
self.f_fssubtype.hash(state);
740+
self.f_fstypename.hash(state);
741+
self.f_type.hash(state);
742+
self.f_mntonname.hash(state);
743+
self.f_mntfromname.hash(state);
744+
self.f_reserved.hash(state);
745+
}
746+
}
747+
#[cfg(feature = "extra_traits")]
705748
impl PartialEq for dirent {
706749
fn eq(&self, other: &dirent) -> bool {
707750
self.d_ino == other.d_ino
@@ -732,6 +775,17 @@ impl std::fmt::Debug for dirent {
732775
}
733776
}
734777
#[cfg(feature = "extra_traits")]
778+
impl std::hash::Hash for dirent {
779+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
780+
self.d_ino.hash(state);
781+
self.d_seekoff.hash(state);
782+
self.d_reclen.hash(state);
783+
self.d_namlen.hash(state);
784+
self.d_type.hash(state);
785+
self.d_name.hash(state);
786+
}
787+
}
788+
#[cfg(feature = "extra_traits")]
735789
impl PartialEq for pthread_rwlock_t {
736790
fn eq(&self, other: &pthread_rwlock_t) -> bool {
737791
self.__sig == other.__sig
@@ -754,6 +808,13 @@ impl std::fmt::Debug for pthread_rwlock_t {
754808
}
755809
}
756810
#[cfg(feature = "extra_traits")]
811+
impl std::hash::Hash for pthread_rwlock_t {
812+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
813+
self.__sig.hash(state);
814+
self.__opaque.hash(state);
815+
}
816+
}
817+
#[cfg(feature = "extra_traits")]
757818
impl PartialEq for pthread_mutex_t {
758819
fn eq(&self, other: &pthread_mutex_t) -> bool {
759820
self.__sig == other.__sig
@@ -776,6 +837,13 @@ impl std::fmt::Debug for pthread_mutex_t {
776837
}
777838
}
778839
#[cfg(feature = "extra_traits")]
840+
impl std::hash::Hash for pthread_mutex_t {
841+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
842+
self.__sig.hash(state);
843+
self.__opaque.hash(state);
844+
}
845+
}
846+
#[cfg(feature = "extra_traits")]
779847
impl PartialEq for pthread_cond_t {
780848
fn eq(&self, other: &pthread_cond_t) -> bool {
781849
self.__sig == other.__sig
@@ -798,6 +866,13 @@ impl std::fmt::Debug for pthread_cond_t {
798866
}
799867
}
800868
#[cfg(feature = "extra_traits")]
869+
impl std::hash::Hash for pthread_cond_t {
870+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
871+
self.__sig.hash(state);
872+
self.__opaque.hash(state);
873+
}
874+
}
875+
#[cfg(feature = "extra_traits")]
801876
impl PartialEq for sockaddr_storage {
802877
fn eq(&self, other: &sockaddr_storage) -> bool {
803878
self.ss_len == other.ss_len
@@ -830,6 +905,16 @@ impl std::fmt::Debug for sockaddr_storage {
830905
}
831906
}
832907
#[cfg(feature = "extra_traits")]
908+
impl std::hash::Hash for sockaddr_storage {
909+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
910+
self.ss_len.hash(state);
911+
self.ss_family.hash(state);
912+
self.__ss_pad1.hash(state);
913+
self.__ss_align.hash(state);
914+
self.__ss_pad2.hash(state);
915+
}
916+
}
917+
#[cfg(feature = "extra_traits")]
833918
impl PartialEq for utmpx {
834919
fn eq(&self, other: &utmpx) -> bool {
835920
self.ut_user
@@ -866,6 +951,19 @@ impl std::fmt::Debug for utmpx {
866951
.finish()
867952
}
868953
}
954+
#[cfg(feature = "extra_traits")]
955+
impl std::hash::Hash for utmpx {
956+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
957+
self.ut_user.hash(state);
958+
self.ut_id.hash(state);
959+
self.ut_line.hash(state);
960+
self.ut_pid.hash(state);
961+
self.ut_type.hash(state);
962+
self.ut_tv.hash(state);
963+
self.ut_host.hash(state);
964+
self.ut_pad.hash(state);
965+
}
966+
}
869967

870968
pub const _UTX_USERSIZE: usize = 256;
871969
pub const _UTX_LINESIZE: usize = 32;

src/unix/bsd/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ impl std::fmt::Debug for sockaddr_un {
162162
}
163163
}
164164
#[cfg(feature = "extra_traits")]
165+
impl std::hash::Hash for sockaddr_un {
166+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
167+
self.sun_len.hash(state);
168+
self.sun_family.hash(state);
169+
self.sun_path.hash(state);
170+
}
171+
}
172+
#[cfg(feature = "extra_traits")]
165173
impl PartialEq for utsname {
166174
fn eq(&self, other: &utsname) -> bool {
167175
self.sysname
@@ -204,6 +212,16 @@ impl std::fmt::Debug for utsname {
204212
.finish()
205213
}
206214
}
215+
#[cfg(feature = "extra_traits")]
216+
impl std::hash::Hash for utsname {
217+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
218+
self.sysname.hash(state);
219+
self.nodename.hash(state);
220+
self.release.hash(state);
221+
self.version.hash(state);
222+
self.machine.hash(state);
223+
}
224+
}
207225

208226
pub const LC_ALL: ::c_int = 0;
209227
pub const LC_COLLATE: ::c_int = 1;

src/unix/notbsd/android/b64/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ impl std::fmt::Debug for pthread_mutex_t {
150150
}
151151
}
152152
#[cfg(feature = "extra_traits")]
153+
impl std::hash::Hash for pthread_mutex_t {
154+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
155+
self.value.hash(state);
156+
self.__reserved.hash(state);
157+
}
158+
}
159+
#[cfg(feature = "extra_traits")]
153160
impl PartialEq for pthread_cond_t {
154161
fn eq(&self, other: &pthread_cond_t) -> bool {
155162
self.value == other.value
@@ -172,6 +179,13 @@ impl std::fmt::Debug for pthread_cond_t {
172179
}
173180
}
174181
#[cfg(feature = "extra_traits")]
182+
impl std::hash::Hash for pthread_cond_t {
183+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
184+
self.value.hash(state);
185+
self.__reserved.hash(state);
186+
}
187+
}
188+
#[cfg(feature = "extra_traits")]
175189
impl PartialEq for pthread_rwlock_t {
176190
fn eq(&self, other: &pthread_rwlock_t) -> bool {
177191
self.numLocks == other.numLocks
@@ -201,6 +215,17 @@ impl std::fmt::Debug for pthread_rwlock_t {
201215
.finish()
202216
}
203217
}
218+
#[cfg(feature = "extra_traits")]
219+
impl std::hash::Hash for pthread_rwlock_t {
220+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
221+
self.numLocks.hash(state);
222+
self.writerThreadId.hash(state);
223+
self.pendingReaders.hash(state);
224+
self.pendingWriters.hash(state);
225+
self.attr.hash(state);
226+
self.__reserved.hash(state);
227+
}
228+
}
204229

205230
pub const RTLD_GLOBAL: ::c_int = 0x00100;
206231
pub const RTLD_NOW: ::c_int = 2;

src/unix/notbsd/android/mod.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,16 @@ impl std::fmt::Debug for dirent {
269269
}
270270
}
271271
#[cfg(feature = "extra_traits")]
272+
impl std::hash::Hash for dirent {
273+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
274+
self.d_ino.hash(state);
275+
self.d_off.hash(state);
276+
self.d_reclen.hash(state);
277+
self.d_type.hash(state);
278+
self.d_name.hash(state);
279+
}
280+
}
281+
#[cfg(feature = "extra_traits")]
272282
impl PartialEq for dirent64 {
273283
fn eq(&self, other: &dirent64) -> bool {
274284
self.d_ino == other.d_ino
@@ -297,6 +307,16 @@ impl std::fmt::Debug for dirent64 {
297307
}
298308
}
299309
#[cfg(feature = "extra_traits")]
310+
impl std::hash::Hash for dirent64 {
311+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
312+
self.d_ino.hash(state);
313+
self.d_off.hash(state);
314+
self.d_reclen.hash(state);
315+
self.d_type.hash(state);
316+
self.d_name.hash(state);
317+
}
318+
}
319+
#[cfg(feature = "extra_traits")]
300320
impl PartialEq for siginfo_t {
301321
fn eq(&self, other: &siginfo_t) -> bool {
302322
self.si_signo == other.si_signo
@@ -321,6 +341,16 @@ impl std::fmt::Debug for siginfo_t {
321341
}
322342
}
323343
#[cfg(feature = "extra_traits")]
344+
impl std::hash::Hash for siginfo_t {
345+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
346+
self.si_signo.hash(state);
347+
self.si_errno.hash(state);
348+
self.si_code.hash(state);
349+
// Ignore _pad
350+
// Ignore _align
351+
}
352+
}
353+
#[cfg(feature = "extra_traits")]
324354
impl PartialEq for lastlog {
325355
fn eq(&self, other: &lastlog) -> bool {
326356
self.ll_time == other.ll_time
@@ -349,6 +379,14 @@ impl std::fmt::Debug for lastlog {
349379
}
350380
}
351381
#[cfg(feature = "extra_traits")]
382+
impl std::hash::Hash for lastlog {
383+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
384+
self.ll_time.hash(state);
385+
self.ll_line.hash(state);
386+
self.ll_host.hash(state);
387+
}
388+
}
389+
#[cfg(feature = "extra_traits")]
352390
impl PartialEq for utmp {
353391
fn eq(&self, other: &utmp) -> bool {
354392
self.ut_type == other.ut_type
@@ -396,6 +434,22 @@ impl std::fmt::Debug for utmp {
396434
.finish()
397435
}
398436
}
437+
#[cfg(feature = "extra_traits")]
438+
impl std::hash::Hash for utmp {
439+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
440+
self.ut_type.hash(state);
441+
self.ut_pid.hash(state);
442+
self.ut_line.hash(state);
443+
self.ut_id.hash(state);
444+
self.ut_user.hash(state);
445+
self.ut_host.hash(state);
446+
self.ut_exit.hash(state);
447+
self.ut_session.hash(state);
448+
self.ut_tv.hash(state);
449+
self.ut_addr_v6.hash(state);
450+
self.unused.hash(state);
451+
}
452+
}
399453

400454
pub const O_TRUNC: ::c_int = 512;
401455
pub const O_CLOEXEC: ::c_int = 0x80000;

0 commit comments

Comments
 (0)