Skip to content

Commit d0ff916

Browse files
committed
Implement Hash for all types
1 parent 0e1ee5c commit d0ff916

File tree

6 files changed

+109
-3
lines changed

6 files changed

+109
-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
@@ -40,7 +40,7 @@ macro_rules! s {
4040
#[repr(C)]
4141
$(#[$attr])*
4242
#[derive(Clone, Copy)]
43-
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, PartialEq))]
43+
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
4444
pub $t $i { $($field)* }
4545
}
4646
)*)

src/unix/notbsd/linux/mod.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,16 @@ impl std::fmt::Debug for dirent {
683683
.finish()
684684
}
685685
}
686+
#[cfg(feature = "extra_traits")]
687+
impl std::hash::Hash for dirent {
688+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
689+
self.d_ino.hash(state);
690+
self.d_off.hash(state);
691+
self.d_reclen.hash(state);
692+
self.d_type.hash(state);
693+
self.d_name.hash(state);
694+
}
695+
}
686696

687697
#[cfg(feature = "extra_traits")]
688698
impl PartialEq for dirent64 {
@@ -708,6 +718,16 @@ impl std::fmt::Debug for dirent64 {
708718
.finish()
709719
}
710720
}
721+
#[cfg(feature = "extra_traits")]
722+
impl std::hash::Hash for dirent64 {
723+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
724+
self.d_ino.hash(state);
725+
self.d_off.hash(state);
726+
self.d_reclen.hash(state);
727+
self.d_type.hash(state);
728+
self.d_name.hash(state);
729+
}
730+
}
711731

712732
#[cfg(feature = "extra_traits")]
713733
impl PartialEq for pthread_cond_t {
@@ -725,6 +745,12 @@ impl std::fmt::Debug for pthread_cond_t {
725745
.finish()
726746
}
727747
}
748+
#[cfg(feature = "extra_traits")]
749+
impl std::hash::Hash for pthread_cond_t {
750+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
751+
self.size.hash(state);
752+
}
753+
}
728754

729755
#[cfg(feature = "extra_traits")]
730756
impl PartialEq for pthread_mutex_t {
@@ -742,6 +768,12 @@ impl std::fmt::Debug for pthread_mutex_t {
742768
.finish()
743769
}
744770
}
771+
#[cfg(feature = "extra_traits")]
772+
impl std::hash::Hash for pthread_mutex_t {
773+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
774+
self.size.hash(state);
775+
}
776+
}
745777

746778
#[cfg(feature = "extra_traits")]
747779
impl PartialEq for pthread_rwlock_t {
@@ -759,6 +791,12 @@ impl std::fmt::Debug for pthread_rwlock_t {
759791
.finish()
760792
}
761793
}
794+
#[cfg(feature = "extra_traits")]
795+
impl std::hash::Hash for pthread_rwlock_t {
796+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
797+
self.size.hash(state);
798+
}
799+
}
762800

763801
pub const ABDAY_1: ::nl_item = 0x20000;
764802
pub const ABDAY_2: ::nl_item = 0x20001;

src/unix/notbsd/linux/other/b64/x86_64.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,21 @@ impl std::fmt::Debug for user_fpregs_struct {
272272
.finish()
273273
}
274274
}
275+
#[cfg(feature = "extra_traits")]
276+
impl std::hash::Hash for user_fpregs_struct {
277+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
278+
self.cwd.hash(state);
279+
self.ftw.hash(state);
280+
self.fop.hash(state);
281+
self.rip.hash(state);
282+
self.rdp.hash(state);
283+
self.mxcsr.hash(state);
284+
self.mxcr_mask.hash(state);
285+
self.st_space.hash(state);
286+
self.xmm_space.hash(state);
287+
// Ignore padding field
288+
}
289+
}
275290

276291
#[cfg(feature = "extra_traits")]
277292
impl PartialEq for ucontext_t {
@@ -299,6 +314,17 @@ impl std::fmt::Debug for ucontext_t {
299314
.finish()
300315
}
301316
}
317+
#[cfg(feature = "extra_traits")]
318+
impl std::hash::Hash for ucontext_t {
319+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
320+
self.uc_flags.hash(state);
321+
self.uc_link.hash(state);
322+
self.uc_stack.hash(state);
323+
self.uc_mcontext.hash(state);
324+
self.uc_sigmask.hash(state);
325+
// Ignore __private field
326+
}
327+
}
302328

303329
pub const TIOCGSOFTCAR: ::c_ulong = 0x5419;
304330
pub const TIOCSSOFTCAR: ::c_ulong = 0x541A;

src/unix/notbsd/linux/other/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,22 @@ impl std::fmt::Debug for utmpx {
282282
.finish()
283283
}
284284
}
285+
#[cfg(feature = "extra_traits")]
286+
impl std::hash::Hash for utmpx {
287+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
288+
self.ut_type.hash(state);
289+
self.ut_pid.hash(state);
290+
self.ut_line.hash(state);
291+
self.ut_id.hash(state);
292+
self.ut_user.hash(state);
293+
self.ut_host.hash(state);
294+
self.ut_exit.hash(state);
295+
self.ut_session.hash(state);
296+
self.ut_tv.hash(state);
297+
self.ut_addr_v6.hash(state);
298+
self.__glibc_reserved.hash(state);
299+
}
300+
}
285301

286302
pub const __UT_LINESIZE: usize = 32;
287303
pub const __UT_NAMESIZE: usize = 32;

src/unix/notbsd/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ impl std::fmt::Debug for sockaddr_un {
254254
.finish()
255255
}
256256
}
257+
#[cfg(feature = "extra_traits")]
258+
impl std::hash::Hash for sockaddr_un {
259+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
260+
self.sun_family.hash(state);
261+
self.sun_path.hash(state);
262+
}
263+
}
257264

258265
#[cfg(feature = "extra_traits")]
259266
impl PartialEq for sockaddr_storage {
@@ -277,6 +284,13 @@ impl std::fmt::Debug for sockaddr_storage {
277284
.finish()
278285
}
279286
}
287+
#[cfg(feature = "extra_traits")]
288+
impl std::hash::Hash for sockaddr_storage {
289+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
290+
self.ss_family.hash(state);
291+
self.__ss_pad2.hash(state);
292+
}
293+
}
280294

281295
#[cfg(feature = "extra_traits")]
282296
impl PartialEq for utsname {
@@ -327,6 +341,17 @@ impl std::fmt::Debug for utsname {
327341
.finish()
328342
}
329343
}
344+
#[cfg(feature = "extra_traits")]
345+
impl std::hash::Hash for utsname {
346+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
347+
self.sysname.hash(state);
348+
self.nodename.hash(state);
349+
self.release.hash(state);
350+
self.version.hash(state);
351+
self.machine.hash(state);
352+
self.domainname.hash(state);
353+
}
354+
}
330355

331356
// intentionally not public, only used for fd_set
332357
cfg_if! {

0 commit comments

Comments
 (0)