Implement Hash for all types

This commit is contained in:
Bryant Mairs 2019-01-21 19:49:48 -08:00
parent 79ae1217c2
commit cd1e16d1af
17 changed files with 394 additions and 3 deletions

View File

@ -45,8 +45,9 @@ libc = { version = "0.2", features = ["align"] }
```
All structs implemented by the libc crate have the `Copy` and `Clone` traits
implemented for them. The additional traits of `Debug, `Eq`, and `PartialEq`
can be enabled with the *extra_traits* feature (requires Rust 1.25 or newer):
implemented for them. The additional traits of `Debug, `Eq`, `Hash`, and
`PartialEq` can be enabled with the *extra_traits* feature (requires Rust 1.25
or newer):
```toml
[dependencies]

View File

@ -39,7 +39,7 @@ macro_rules! s {
__item! {
#[repr(C)]
$(#[$attr])*
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, PartialEq))]
#[cfg_attr(feature = "extra_traits", derive(Debug, Eq, Hash, PartialEq))]
pub $t $i { $($field)* }
}
impl ::dox::Copy for $i {}

View File

@ -73,6 +73,13 @@ impl std::fmt::Debug for pthread_attr_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_attr_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.__sig.hash(state);
self.__opaque.hash(state);
}
}
pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
pub const __PTHREAD_COND_SIZE__: usize = 24;

View File

@ -78,6 +78,13 @@ impl std::fmt::Debug for pthread_attr_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_attr_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.__sig.hash(state);
self.__opaque.hash(state);
}
}
pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
pub const __PTHREAD_COND_SIZE__: usize = 40;

View File

@ -608,6 +608,12 @@ impl std::fmt::Debug for semun {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for semun {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
unsafe { self.val.hash(state) };
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for proc_threadinfo {
fn eq(&self, other: &proc_threadinfo) -> bool {
self.pth_user_time == other.pth_user_time
@ -648,6 +654,22 @@ impl std::fmt::Debug for proc_threadinfo {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for proc_threadinfo {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.pth_user_time.hash(state);
self.pth_system_time.hash(state);
self.pth_cpu_usage.hash(state);
self.pth_policy.hash(state);
self.pth_run_state.hash(state);
self.pth_flags.hash(state);
self.pth_sleep_time.hash(state);
self.pth_curpri.hash(state);
self.pth_priority.hash(state);
self.pth_maxpriority.hash(state);
self.pth_name.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for statfs {
fn eq(&self, other: &statfs) -> bool {
self.f_bsize == other.f_bsize
@ -702,6 +724,27 @@ impl std::fmt::Debug for statfs {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for statfs {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.f_bsize.hash(state);
self.f_iosize.hash(state);
self.f_blocks.hash(state);
self.f_bfree.hash(state);
self.f_bavail.hash(state);
self.f_files.hash(state);
self.f_ffree.hash(state);
self.f_fsid.hash(state);
self.f_owner.hash(state);
self.f_flags.hash(state);
self.f_fssubtype.hash(state);
self.f_fstypename.hash(state);
self.f_type.hash(state);
self.f_mntonname.hash(state);
self.f_mntfromname.hash(state);
self.f_reserved.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for dirent {
fn eq(&self, other: &dirent) -> bool {
self.d_ino == other.d_ino
@ -732,6 +775,17 @@ impl std::fmt::Debug for dirent {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for dirent {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.d_ino.hash(state);
self.d_seekoff.hash(state);
self.d_reclen.hash(state);
self.d_namlen.hash(state);
self.d_type.hash(state);
self.d_name.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_rwlock_t {
fn eq(&self, other: &pthread_rwlock_t) -> bool {
self.__sig == other.__sig
@ -754,6 +808,13 @@ impl std::fmt::Debug for pthread_rwlock_t {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_rwlock_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.__sig.hash(state);
self.__opaque.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_mutex_t {
fn eq(&self, other: &pthread_mutex_t) -> bool {
self.__sig == other.__sig
@ -776,6 +837,13 @@ impl std::fmt::Debug for pthread_mutex_t {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_mutex_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.__sig.hash(state);
self.__opaque.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_cond_t {
fn eq(&self, other: &pthread_cond_t) -> bool {
self.__sig == other.__sig
@ -798,6 +866,13 @@ impl std::fmt::Debug for pthread_cond_t {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_cond_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.__sig.hash(state);
self.__opaque.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for sockaddr_storage {
fn eq(&self, other: &sockaddr_storage) -> bool {
self.ss_len == other.ss_len
@ -830,6 +905,16 @@ impl std::fmt::Debug for sockaddr_storage {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for sockaddr_storage {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ss_len.hash(state);
self.ss_family.hash(state);
self.__ss_pad1.hash(state);
self.__ss_align.hash(state);
self.__ss_pad2.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for utmpx {
fn eq(&self, other: &utmpx) -> bool {
self.ut_user
@ -866,6 +951,19 @@ impl std::fmt::Debug for utmpx {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for utmpx {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ut_user.hash(state);
self.ut_id.hash(state);
self.ut_line.hash(state);
self.ut_pid.hash(state);
self.ut_type.hash(state);
self.ut_tv.hash(state);
self.ut_host.hash(state);
self.ut_pad.hash(state);
}
}
pub const _UTX_USERSIZE: usize = 256;
pub const _UTX_LINESIZE: usize = 32;

View File

@ -162,6 +162,14 @@ impl std::fmt::Debug for sockaddr_un {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for sockaddr_un {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.sun_len.hash(state);
self.sun_family.hash(state);
self.sun_path.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for utsname {
fn eq(&self, other: &utsname) -> bool {
self.sysname
@ -204,6 +212,16 @@ impl std::fmt::Debug for utsname {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for utsname {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.sysname.hash(state);
self.nodename.hash(state);
self.release.hash(state);
self.version.hash(state);
self.machine.hash(state);
}
}
pub const LC_ALL: ::c_int = 0;
pub const LC_COLLATE: ::c_int = 1;

View File

@ -150,6 +150,13 @@ impl std::fmt::Debug for pthread_mutex_t {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_mutex_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
self.__reserved.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_cond_t {
fn eq(&self, other: &pthread_cond_t) -> bool {
self.value == other.value
@ -172,6 +179,13 @@ impl std::fmt::Debug for pthread_cond_t {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_cond_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.value.hash(state);
self.__reserved.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_rwlock_t {
fn eq(&self, other: &pthread_rwlock_t) -> bool {
self.numLocks == other.numLocks
@ -201,6 +215,17 @@ impl std::fmt::Debug for pthread_rwlock_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_rwlock_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.numLocks.hash(state);
self.writerThreadId.hash(state);
self.pendingReaders.hash(state);
self.pendingWriters.hash(state);
self.attr.hash(state);
self.__reserved.hash(state);
}
}
pub const RTLD_GLOBAL: ::c_int = 0x00100;
pub const RTLD_NOW: ::c_int = 2;

View File

@ -269,6 +269,16 @@ impl std::fmt::Debug for dirent {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for dirent {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.d_ino.hash(state);
self.d_off.hash(state);
self.d_reclen.hash(state);
self.d_type.hash(state);
self.d_name.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for dirent64 {
fn eq(&self, other: &dirent64) -> bool {
self.d_ino == other.d_ino
@ -297,6 +307,16 @@ impl std::fmt::Debug for dirent64 {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for dirent64 {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.d_ino.hash(state);
self.d_off.hash(state);
self.d_reclen.hash(state);
self.d_type.hash(state);
self.d_name.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for siginfo_t {
fn eq(&self, other: &siginfo_t) -> bool {
self.si_signo == other.si_signo
@ -321,6 +341,16 @@ impl std::fmt::Debug for siginfo_t {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for siginfo_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.si_signo.hash(state);
self.si_errno.hash(state);
self.si_code.hash(state);
// Ignore _pad
// Ignore _align
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for lastlog {
fn eq(&self, other: &lastlog) -> bool {
self.ll_time == other.ll_time
@ -349,6 +379,14 @@ impl std::fmt::Debug for lastlog {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for lastlog {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ll_time.hash(state);
self.ll_line.hash(state);
self.ll_host.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for utmp {
fn eq(&self, other: &utmp) -> bool {
self.ut_type == other.ut_type
@ -396,6 +434,22 @@ impl std::fmt::Debug for utmp {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for utmp {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ut_type.hash(state);
self.ut_pid.hash(state);
self.ut_line.hash(state);
self.ut_id.hash(state);
self.ut_user.hash(state);
self.ut_host.hash(state);
self.ut_exit.hash(state);
self.ut_session.hash(state);
self.ut_tv.hash(state);
self.ut_addr_v6.hash(state);
self.unused.hash(state);
}
}
pub const O_TRUNC: ::c_int = 512;
pub const O_CLOEXEC: ::c_int = 0x80000;

View File

@ -687,6 +687,16 @@ impl std::fmt::Debug for dirent {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for dirent {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.d_ino.hash(state);
self.d_off.hash(state);
self.d_reclen.hash(state);
self.d_type.hash(state);
self.d_name.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for dirent64 {
@ -716,6 +726,16 @@ impl std::fmt::Debug for dirent64 {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for dirent64 {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.d_ino.hash(state);
self.d_off.hash(state);
self.d_reclen.hash(state);
self.d_type.hash(state);
self.d_name.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_cond_t {
@ -733,6 +753,12 @@ impl std::fmt::Debug for pthread_cond_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_cond_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.size.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_mutex_t {
@ -750,6 +776,12 @@ impl std::fmt::Debug for pthread_mutex_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_mutex_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.size.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_rwlock_t {
@ -767,6 +799,12 @@ impl std::fmt::Debug for pthread_rwlock_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for pthread_rwlock_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.size.hash(state);
}
}
pub const ABDAY_1: ::nl_item = 0x20000;
pub const ABDAY_2: ::nl_item = 0x20001;

View File

@ -207,6 +207,17 @@ impl std::fmt::Debug for ucontext_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for ucontext_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.uc_flags.hash(state);
self.uc_link.hash(state);
self.uc_stack.hash(state);
self.uc_mcontext.hash(state);
self.uc_sigmask.hash(state);
self.__private.hash(state);
}
}
pub const SIGSTKSZ: ::size_t = 8192;
pub const MINSIGSTKSZ: ::size_t = 2048;

View File

@ -105,6 +105,17 @@ impl std::fmt::Debug for ucontext_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for ucontext_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.uc_flags.hash(state);
self.uc_link.hash(state);
self.uc_stack.hash(state);
self.uc_mcontext.hash(state);
self.uc_sigmask.hash(state);
self.__private.hash(state);
}
}
// Syscall table

View File

@ -141,6 +141,25 @@ impl std::fmt::Debug for sysinfo {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for sysinfo {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.uptime.hash(state);
self.loads.hash(state);
self.totalram.hash(state);
self.freeram.hash(state);
self.sharedram.hash(state);
self.bufferram.hash(state);
self.totalswap.hash(state);
self.freeswap.hash(state);
self.procs.hash(state);
self.pad.hash(state);
self.totalhigh.hash(state);
self.freehigh.hash(state);
self.mem_unit.hash(state);
self.__reserved.hash(state);
}
}
pub const SFD_CLOEXEC: ::c_int = 0x080000;

View File

@ -256,6 +256,24 @@ impl std::fmt::Debug for user_fpxregs_struct {
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for user_fpxregs_struct {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.cwd.hash(state);
self.swd.hash(state);
self.twd.hash(state);
self.fop.hash(state);
self.fip.hash(state);
self.fcs.hash(state);
self.foo.hash(state);
self.fos.hash(state);
self.mxcsr.hash(state);
// Ignore __reserved field
self.st_space.hash(state);
self.xmm_space.hash(state);
// Ignore padding field
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for ucontext_t {
fn eq(&self, other: &ucontext_t) -> bool {
self.uc_flags == other.uc_flags
@ -281,6 +299,17 @@ impl std::fmt::Debug for ucontext_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for ucontext_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.uc_flags.hash(state);
self.uc_link.hash(state);
self.uc_stack.hash(state);
self.uc_mcontext.hash(state);
self.uc_sigmask.hash(state);
// Ignore __private field
}
}
pub const O_DIRECT: ::c_int = 0x4000;
pub const O_DIRECTORY: ::c_int = 0x10000;

View File

@ -273,6 +273,21 @@ impl std::fmt::Debug for user_fpregs_struct {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for user_fpregs_struct {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.cwd.hash(state);
self.ftw.hash(state);
self.fop.hash(state);
self.rip.hash(state);
self.rdp.hash(state);
self.mxcsr.hash(state);
self.mxcr_mask.hash(state);
self.st_space.hash(state);
self.xmm_space.hash(state);
// Ignore padding field
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for ucontext_t {
@ -300,6 +315,17 @@ impl std::fmt::Debug for ucontext_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for ucontext_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.uc_flags.hash(state);
self.uc_link.hash(state);
self.uc_stack.hash(state);
self.uc_mcontext.hash(state);
self.uc_sigmask.hash(state);
// Ignore __private field
}
}
pub const TIOCGSOFTCAR: ::c_ulong = 0x5419;
pub const TIOCSSOFTCAR: ::c_ulong = 0x541A;

View File

@ -286,6 +286,22 @@ impl std::fmt::Debug for utmpx {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for utmpx {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ut_type.hash(state);
self.ut_pid.hash(state);
self.ut_line.hash(state);
self.ut_id.hash(state);
self.ut_user.hash(state);
self.ut_host.hash(state);
self.ut_exit.hash(state);
self.ut_session.hash(state);
self.ut_tv.hash(state);
self.ut_addr_v6.hash(state);
self.__glibc_reserved.hash(state);
}
}
pub const __UT_LINESIZE: usize = 32;
pub const __UT_NAMESIZE: usize = 32;

View File

@ -352,6 +352,12 @@ impl std::fmt::Debug for fpreg_t {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for fpreg_t {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.d.to_bits().hash(state);
}
}
pub const SFD_CLOEXEC: ::c_int = 0x080000;

View File

@ -254,6 +254,13 @@ impl std::fmt::Debug for sockaddr_un {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for sockaddr_un {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.sun_family.hash(state);
self.sun_path.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for sockaddr_storage {
@ -278,6 +285,13 @@ impl std::fmt::Debug for sockaddr_storage {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for sockaddr_storage {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.ss_family.hash(state);
self.__ss_pad2.hash(state);
}
}
#[cfg(feature = "extra_traits")]
impl PartialEq for utsname {
@ -328,6 +342,17 @@ impl std::fmt::Debug for utsname {
.finish()
}
}
#[cfg(feature = "extra_traits")]
impl std::hash::Hash for utsname {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.sysname.hash(state);
self.nodename.hash(state);
self.release.hash(state);
self.version.hash(state);
self.machine.hash(state);
self.domainname.hash(state);
}
}
// intentionally not public, only used for fd_set
cfg_if! {