Skip to content

Commit 177a0a5

Browse files
committed
Implement PartialEq,Eq for all types
1 parent 70284d7 commit 177a0a5

File tree

7 files changed

+359
-193
lines changed

7 files changed

+359
-193
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ default = ["use_std"]
2727
use_std = []
2828
align = []
2929
rustc-dep-of-std = ['align', 'rustc-std-workspace-core']
30+
extra_traits = []
3031

3132
[workspace]
3233
members = ["libc-test"]

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ activate the *align* feature. This requires Rust 1.25 or newer:
4444
libc = { version = "0.2", features = ["align"] }
4545
```
4646

47+
All structs implemented by the libc crate have the `Copy` and `Clone` traits
48+
implemented for them. The additional traits of `PartialEq` and `Eq` can be
49+
enabled with the *extra_traits* feature:
50+
51+
```toml
52+
[dependencies]
53+
libc = { version = "0.2", features = ["extra_traits"] }
54+
```
55+
4756
## What is libc?
4857

4958
The primary purpose of this crate is to provide all of the definitions necessary

src/macros.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,20 @@ macro_rules! s {
3939
__item! {
4040
#[repr(C)]
4141
$(#[$attr])*
42+
#[derive(Clone, Copy)]
43+
#[cfg_attr(feature = "extra_traits", derive(Eq, PartialEq))]
4244
pub $t $i { $($field)* }
4345
}
44-
impl ::dox::Copy for $i {}
45-
impl ::dox::Clone for $i {
46-
fn clone(&self) -> $i { *self }
46+
)*)
47+
}
48+
49+
macro_rules! s_no_extra_traits {
50+
($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
51+
__item! {
52+
#[repr(C)]
53+
$(#[$attr])*
54+
#[derive(Clone, Copy)]
55+
pub $t $i { $($field)* }
4756
}
4857
)*)
4958
}

src/unix/notbsd/linux/mod.rs

+163-108
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,6 @@ pub type Elf64_Section = u16;
4141
pub enum fpos64_t {} // TODO: fill this out with a struct
4242

4343
s! {
44-
pub struct dirent {
45-
pub d_ino: ::ino_t,
46-
pub d_off: ::off_t,
47-
pub d_reclen: ::c_ushort,
48-
pub d_type: ::c_uchar,
49-
pub d_name: [::c_char; 256],
50-
}
51-
52-
pub struct dirent64 {
53-
pub d_ino: ::ino64_t,
54-
pub d_off: ::off64_t,
55-
pub d_reclen: ::c_ushort,
56-
pub d_type: ::c_uchar,
57-
pub d_name: [::c_char; 256],
58-
}
59-
6044
pub struct rlimit64 {
6145
pub rlim_cur: rlim64_t,
6246
pub rlim_max: rlim64_t,
@@ -75,74 +59,6 @@ s! {
7559
__unused5: *mut ::c_void,
7660
}
7761

78-
#[cfg_attr(all(feature = "align",
79-
target_pointer_width = "32",
80-
any(target_arch = "mips",
81-
target_arch = "arm",
82-
target_arch = "powerpc",
83-
target_arch = "x86_64",
84-
target_arch = "x86")),
85-
repr(align(4)))]
86-
#[cfg_attr(all(feature = "align",
87-
any(target_pointer_width = "64",
88-
not(any(target_arch = "mips",
89-
target_arch = "arm",
90-
target_arch = "powerpc",
91-
target_arch = "x86_64",
92-
target_arch = "x86")))),
93-
repr(align(8)))]
94-
pub struct pthread_mutex_t {
95-
#[cfg(all(not(feature = "align"),
96-
any(target_arch = "mips",
97-
target_arch = "arm",
98-
target_arch = "powerpc",
99-
all(target_arch = "x86_64",
100-
target_pointer_width = "32"))))]
101-
__align: [::c_long; 0],
102-
#[cfg(not(any(feature = "align",
103-
target_arch = "mips",
104-
target_arch = "arm",
105-
target_arch = "powerpc",
106-
all(target_arch = "x86_64",
107-
target_pointer_width = "32"))))]
108-
__align: [::c_longlong; 0],
109-
size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
110-
}
111-
112-
#[cfg_attr(all(feature = "align",
113-
target_pointer_width = "32",
114-
any(target_arch = "mips",
115-
target_arch = "arm",
116-
target_arch = "powerpc",
117-
target_arch = "x86_64",
118-
target_arch = "x86")),
119-
repr(align(4)))]
120-
#[cfg_attr(all(feature = "align",
121-
any(target_pointer_width = "64",
122-
not(any(target_arch = "mips",
123-
target_arch = "arm",
124-
target_arch = "powerpc",
125-
target_arch = "x86_64",
126-
target_arch = "x86")))),
127-
repr(align(8)))]
128-
pub struct pthread_rwlock_t {
129-
#[cfg(all(not(feature = "align"),
130-
any(target_arch = "mips",
131-
target_arch = "arm",
132-
target_arch = "powerpc",
133-
all(target_arch = "x86_64",
134-
target_pointer_width = "32"))))]
135-
__align: [::c_long; 0],
136-
#[cfg(not(any(feature = "align",
137-
target_arch = "mips",
138-
target_arch = "arm",
139-
target_arch = "powerpc",
140-
all(target_arch = "x86_64",
141-
target_pointer_width = "32"))))]
142-
__align: [::c_longlong; 0],
143-
size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
144-
}
145-
14662
#[cfg_attr(all(feature = "align",
14763
any(target_pointer_width = "32",
14864
target_arch = "x86_64", target_arch = "powerpc64",
@@ -188,30 +104,6 @@ s! {
188104
size: [u8; __SIZEOF_PTHREAD_RWLOCKATTR_T],
189105
}
190106

191-
#[cfg_attr(all(feature = "align",
192-
target_env = "musl",
193-
target_pointer_width = "32"),
194-
repr(align(4)))]
195-
#[cfg_attr(all(feature = "align",
196-
target_env = "musl",
197-
target_pointer_width = "64"),
198-
repr(align(8)))]
199-
#[cfg_attr(all(feature = "align",
200-
not(target_env = "musl"),
201-
target_arch = "x86"),
202-
repr(align(4)))]
203-
#[cfg_attr(all(feature = "align",
204-
not(target_env = "musl"),
205-
not(target_arch = "x86")),
206-
repr(align(8)))]
207-
pub struct pthread_cond_t {
208-
#[cfg(all(not(feature = "align"), target_env = "musl"))]
209-
__align: [*const ::c_void; 0],
210-
#[cfg(not(any(feature = "align", target_env = "musl")))]
211-
__align: [::c_longlong; 0],
212-
size: [u8; __SIZEOF_PTHREAD_COND_T],
213-
}
214-
215107
#[cfg_attr(feature = "align", repr(align(4)))]
216108
pub struct pthread_condattr_t {
217109
#[cfg(not(feature = "align"))]
@@ -657,6 +549,169 @@ s! {
657549
}
658550
}
659551

552+
s_no_extra_traits!{
553+
pub struct dirent {
554+
pub d_ino: ::ino_t,
555+
pub d_off: ::off_t,
556+
pub d_reclen: ::c_ushort,
557+
pub d_type: ::c_uchar,
558+
pub d_name: [::c_char; 256],
559+
}
560+
561+
pub struct dirent64 {
562+
pub d_ino: ::ino64_t,
563+
pub d_off: ::off64_t,
564+
pub d_reclen: ::c_ushort,
565+
pub d_type: ::c_uchar,
566+
pub d_name: [::c_char; 256],
567+
}
568+
569+
#[cfg_attr(all(feature = "align",
570+
target_env = "musl",
571+
target_pointer_width = "32"),
572+
repr(align(4)))]
573+
#[cfg_attr(all(feature = "align",
574+
target_env = "musl",
575+
target_pointer_width = "64"),
576+
repr(align(8)))]
577+
#[cfg_attr(all(feature = "align",
578+
not(target_env = "musl"),
579+
target_arch = "x86"),
580+
repr(align(4)))]
581+
#[cfg_attr(all(feature = "align",
582+
not(target_env = "musl"),
583+
not(target_arch = "x86")),
584+
repr(align(8)))]
585+
pub struct pthread_cond_t {
586+
#[cfg(all(not(feature = "align"), target_env = "musl"))]
587+
__align: [*const ::c_void; 0],
588+
#[cfg(not(any(feature = "align", target_env = "musl")))]
589+
__align: [::c_longlong; 0],
590+
size: [u8; __SIZEOF_PTHREAD_COND_T],
591+
}
592+
593+
#[cfg_attr(all(feature = "align",
594+
target_pointer_width = "32",
595+
any(target_arch = "mips",
596+
target_arch = "arm",
597+
target_arch = "powerpc",
598+
target_arch = "x86_64",
599+
target_arch = "x86")),
600+
repr(align(4)))]
601+
#[cfg_attr(all(feature = "align",
602+
any(target_pointer_width = "64",
603+
not(any(target_arch = "mips",
604+
target_arch = "arm",
605+
target_arch = "powerpc",
606+
target_arch = "x86_64",
607+
target_arch = "x86")))),
608+
repr(align(8)))]
609+
pub struct pthread_mutex_t {
610+
#[cfg(all(not(feature = "align"),
611+
any(target_arch = "mips",
612+
target_arch = "arm",
613+
target_arch = "powerpc",
614+
all(target_arch = "x86_64",
615+
target_pointer_width = "32"))))]
616+
__align: [::c_long; 0],
617+
#[cfg(not(any(feature = "align",
618+
target_arch = "mips",
619+
target_arch = "arm",
620+
target_arch = "powerpc",
621+
all(target_arch = "x86_64",
622+
target_pointer_width = "32"))))]
623+
__align: [::c_longlong; 0],
624+
size: [u8; __SIZEOF_PTHREAD_MUTEX_T],
625+
}
626+
627+
#[cfg_attr(all(feature = "align",
628+
target_pointer_width = "32",
629+
any(target_arch = "mips",
630+
target_arch = "arm",
631+
target_arch = "powerpc",
632+
target_arch = "x86_64",
633+
target_arch = "x86")),
634+
repr(align(4)))]
635+
#[cfg_attr(all(feature = "align",
636+
any(target_pointer_width = "64",
637+
not(any(target_arch = "mips",
638+
target_arch = "arm",
639+
target_arch = "powerpc",
640+
target_arch = "x86_64",
641+
target_arch = "x86")))),
642+
repr(align(8)))]
643+
pub struct pthread_rwlock_t {
644+
#[cfg(all(not(feature = "align"),
645+
any(target_arch = "mips",
646+
target_arch = "arm",
647+
target_arch = "powerpc",
648+
all(target_arch = "x86_64",
649+
target_pointer_width = "32"))))]
650+
__align: [::c_long; 0],
651+
#[cfg(not(any(feature = "align",
652+
target_arch = "mips",
653+
target_arch = "arm",
654+
target_arch = "powerpc",
655+
all(target_arch = "x86_64",
656+
target_pointer_width = "32"))))]
657+
__align: [::c_longlong; 0],
658+
size: [u8; __SIZEOF_PTHREAD_RWLOCK_T],
659+
}
660+
}
661+
662+
#[cfg(feature = "extra_traits")]
663+
impl PartialEq for dirent {
664+
fn eq(&self, other: &dirent) -> bool {
665+
self.d_ino == other.d_ino &&
666+
self.d_off == other.d_off &&
667+
self.d_reclen == other.d_reclen &&
668+
self.d_type == other.d_type &&
669+
self.d_name.iter().zip(other.d_name.iter()).all(|(a,b)| a == b)
670+
}
671+
}
672+
#[cfg(feature = "extra_traits")]
673+
impl Eq for dirent {}
674+
675+
#[cfg(feature = "extra_traits")]
676+
impl PartialEq for dirent64 {
677+
fn eq(&self, other: &dirent64) -> bool {
678+
self.d_ino == other.d_ino &&
679+
self.d_off == other.d_off &&
680+
self.d_reclen == other.d_reclen &&
681+
self.d_type == other.d_type &&
682+
self.d_name.iter().zip(other.d_name.iter()).all(|(a,b)| a == b)
683+
}
684+
}
685+
#[cfg(feature = "extra_traits")]
686+
impl Eq for dirent64 {}
687+
688+
#[cfg(feature = "extra_traits")]
689+
impl PartialEq for pthread_cond_t {
690+
fn eq(&self, other: &pthread_cond_t) -> bool {
691+
self.size.iter().zip(other.size.iter()).all(|(a,b)| a == b)
692+
}
693+
}
694+
#[cfg(feature = "extra_traits")]
695+
impl Eq for pthread_cond_t {}
696+
697+
#[cfg(feature = "extra_traits")]
698+
impl PartialEq for pthread_mutex_t {
699+
fn eq(&self, other: &pthread_mutex_t) -> bool {
700+
self.size.iter().zip(other.size.iter()).all(|(a,b)| a == b)
701+
}
702+
}
703+
#[cfg(feature = "extra_traits")]
704+
impl Eq for pthread_mutex_t {}
705+
706+
#[cfg(feature = "extra_traits")]
707+
impl PartialEq for pthread_rwlock_t {
708+
fn eq(&self, other: &pthread_rwlock_t) -> bool {
709+
self.size.iter().zip(other.size.iter()).all(|(a,b)| a == b)
710+
}
711+
}
712+
#[cfg(feature = "extra_traits")]
713+
impl Eq for pthread_rwlock_t {}
714+
660715
pub const ABDAY_1: ::nl_item = 0x20000;
661716
pub const ABDAY_2: ::nl_item = 0x20001;
662717
pub const ABDAY_3: ::nl_item = 0x20002;

0 commit comments

Comments
 (0)