Skip to content

Commit

Permalink
Implement PartialEq,Eq for all types
Browse files Browse the repository at this point in the history
  • Loading branch information
Susurrus committed Jan 19, 2019
1 parent 70284d7 commit 177a0a5
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 193 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ default = ["use_std"]
use_std = []
align = []
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']
extra_traits = []

[workspace]
members = ["libc-test"]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ activate the *align* feature. This requires Rust 1.25 or newer:
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 `PartialEq` and `Eq` can be
enabled with the *extra_traits* feature:

```toml
[dependencies]
libc = { version = "0.2", features = ["extra_traits"] }
```

## What is libc?

The primary purpose of this crate is to provide all of the definitions necessary
Expand Down
15 changes: 12 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ macro_rules! s {
__item! {
#[repr(C)]
$(#[$attr])*
#[derive(Clone, Copy)]
#[cfg_attr(feature = "extra_traits", derive(Eq, PartialEq))]
pub $t $i { $($field)* }
}
impl ::dox::Copy for $i {}
impl ::dox::Clone for $i {
fn clone(&self) -> $i { *self }
)*)
}

macro_rules! s_no_extra_traits {
($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
__item! {
#[repr(C)]
$(#[$attr])*
#[derive(Clone, Copy)]
pub $t $i { $($field)* }
}
)*)
}
Expand Down
271 changes: 163 additions & 108 deletions src/unix/notbsd/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,6 @@ pub type Elf64_Section = u16;
pub enum fpos64_t {} // TODO: fill this out with a struct

s! {
pub struct dirent {
pub d_ino: ::ino_t,
pub d_off: ::off_t,
pub d_reclen: ::c_ushort,
pub d_type: ::c_uchar,
pub d_name: [::c_char; 256],
}

pub struct dirent64 {
pub d_ino: ::ino64_t,
pub d_off: ::off64_t,
pub d_reclen: ::c_ushort,
pub d_type: ::c_uchar,
pub d_name: [::c_char; 256],
}

pub struct rlimit64 {
pub rlim_cur: rlim64_t,
pub rlim_max: rlim64_t,
Expand All @@ -75,74 +59,6 @@ s! {
__unused5: *mut ::c_void,
}

#[cfg_attr(all(feature = "align",
target_pointer_width = "32",
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
any(target_pointer_width = "64",
not(any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")))),
repr(align(8)))]
pub struct pthread_mutex_t {
#[cfg(all(not(feature = "align"),
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_long; 0],
#[cfg(not(any(feature = "align",
target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_longlong; 0],
size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
}

#[cfg_attr(all(feature = "align",
target_pointer_width = "32",
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
any(target_pointer_width = "64",
not(any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")))),
repr(align(8)))]
pub struct pthread_rwlock_t {
#[cfg(all(not(feature = "align"),
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_long; 0],
#[cfg(not(any(feature = "align",
target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_longlong; 0],
size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
}

#[cfg_attr(all(feature = "align",
any(target_pointer_width = "32",
target_arch = "x86_64", target_arch = "powerpc64",
Expand Down Expand Up @@ -188,30 +104,6 @@ s! {
size: [u8; __SIZEOF_PTHREAD_RWLOCKATTR_T],
}

#[cfg_attr(all(feature = "align",
target_env = "musl",
target_pointer_width = "32"),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
target_env = "musl",
target_pointer_width = "64"),
repr(align(8)))]
#[cfg_attr(all(feature = "align",
not(target_env = "musl"),
target_arch = "x86"),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
not(target_env = "musl"),
not(target_arch = "x86")),
repr(align(8)))]
pub struct pthread_cond_t {
#[cfg(all(not(feature = "align"), target_env = "musl"))]
__align: [*const ::c_void; 0],
#[cfg(not(any(feature = "align", target_env = "musl")))]
__align: [::c_longlong; 0],
size: [u8; __SIZEOF_PTHREAD_COND_T],
}

#[cfg_attr(feature = "align", repr(align(4)))]
pub struct pthread_condattr_t {
#[cfg(not(feature = "align"))]
Expand Down Expand Up @@ -657,6 +549,169 @@ s! {
}
}

s_no_extra_traits!{
pub struct dirent {
pub d_ino: ::ino_t,
pub d_off: ::off_t,
pub d_reclen: ::c_ushort,
pub d_type: ::c_uchar,
pub d_name: [::c_char; 256],
}

pub struct dirent64 {
pub d_ino: ::ino64_t,
pub d_off: ::off64_t,
pub d_reclen: ::c_ushort,
pub d_type: ::c_uchar,
pub d_name: [::c_char; 256],
}

#[cfg_attr(all(feature = "align",
target_env = "musl",
target_pointer_width = "32"),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
target_env = "musl",
target_pointer_width = "64"),
repr(align(8)))]
#[cfg_attr(all(feature = "align",
not(target_env = "musl"),
target_arch = "x86"),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
not(target_env = "musl"),
not(target_arch = "x86")),
repr(align(8)))]
pub struct pthread_cond_t {
#[cfg(all(not(feature = "align"), target_env = "musl"))]
__align: [*const ::c_void; 0],
#[cfg(not(any(feature = "align", target_env = "musl")))]
__align: [::c_longlong; 0],
size: [u8; __SIZEOF_PTHREAD_COND_T],
}

#[cfg_attr(all(feature = "align",
target_pointer_width = "32",
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
any(target_pointer_width = "64",
not(any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")))),
repr(align(8)))]
pub struct pthread_mutex_t {
#[cfg(all(not(feature = "align"),
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_long; 0],
#[cfg(not(any(feature = "align",
target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_longlong; 0],
size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
}

#[cfg_attr(all(feature = "align",
target_pointer_width = "32",
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")),
repr(align(4)))]
#[cfg_attr(all(feature = "align",
any(target_pointer_width = "64",
not(any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "x86_64",
target_arch = "x86")))),
repr(align(8)))]
pub struct pthread_rwlock_t {
#[cfg(all(not(feature = "align"),
any(target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_long; 0],
#[cfg(not(any(feature = "align",
target_arch = "mips",
target_arch = "arm",
target_arch = "powerpc",
all(target_arch = "x86_64",
target_pointer_width = "32"))))]
__align: [::c_longlong; 0],
size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
}
}

#[cfg(feature = "extra_traits")]
impl PartialEq for dirent {
fn eq(&self, other: &dirent) -> bool {
self.d_ino == other.d_ino &&
self.d_off == other.d_off &&
self.d_reclen == other.d_reclen &&
self.d_type == other.d_type &&
self.d_name.iter().zip(other.d_name.iter()).all(|(a,b)| a == b)
}
}
#[cfg(feature = "extra_traits")]
impl Eq for dirent {}

#[cfg(feature = "extra_traits")]
impl PartialEq for dirent64 {
fn eq(&self, other: &dirent64) -> bool {
self.d_ino == other.d_ino &&
self.d_off == other.d_off &&
self.d_reclen == other.d_reclen &&
self.d_type == other.d_type &&
self.d_name.iter().zip(other.d_name.iter()).all(|(a,b)| a == b)
}
}
#[cfg(feature = "extra_traits")]
impl Eq for dirent64 {}

#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_cond_t {
fn eq(&self, other: &pthread_cond_t) -> bool {
self.size.iter().zip(other.size.iter()).all(|(a,b)| a == b)
}
}
#[cfg(feature = "extra_traits")]
impl Eq for pthread_cond_t {}

#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_mutex_t {
fn eq(&self, other: &pthread_mutex_t) -> bool {
self.size.iter().zip(other.size.iter()).all(|(a,b)| a == b)
}
}
#[cfg(feature = "extra_traits")]
impl Eq for pthread_mutex_t {}

#[cfg(feature = "extra_traits")]
impl PartialEq for pthread_rwlock_t {
fn eq(&self, other: &pthread_rwlock_t) -> bool {
self.size.iter().zip(other.size.iter()).all(|(a,b)| a == b)
}
}
#[cfg(feature = "extra_traits")]
impl Eq for pthread_rwlock_t {}

pub const ABDAY_1: ::nl_item = 0x20000;
pub const ABDAY_2: ::nl_item = 0x20001;
pub const ABDAY_3: ::nl_item = 0x20002;
Expand Down
Loading

0 comments on commit 177a0a5

Please sign in to comment.