Skip to content

Commit 868a85d

Browse files
committed
Implement PartialEq,Eq for all types
1 parent 51a0587 commit 868a85d

File tree

20 files changed

+1149
-423
lines changed

20 files changed

+1149
-423
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
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 = ["align"]
3031

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

README.md

Lines changed: 9 additions & 0 deletions
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 (requires Rust 1.25 or newer):
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

ci/run.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,8 @@ fi
9696
if [ "$(rustc --version | sed -E 's/^rustc 1\.([0-9]*)\..*/\1/')" -ge 25 ]; then
9797
cargo test $opt --features align --manifest-path libc-test/Cargo.toml --target "${TARGET}"
9898
fi
99+
# Test the `extra_traits` feature if this is building on Rust >= 1.25
100+
if [ "$(rustc --version | sed -E 's/^rustc 1\.([0-9]*)\..*/\1/')" -ge 25 ]; then
101+
cargo test $opt --features extra_traits --manifest-path libc-test/Cargo.toml --target "${TARGET}"
102+
fi
99103
exec cargo test $opt --manifest-path libc-test/Cargo.toml --target "${TARGET}"

libc-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ctest = "0.2.8"
1515
default = [ "use_std" ]
1616
use_std = [ "libc/use_std" ]
1717
align = [ "libc/align" ]
18+
extra_traits = [ "libc/extra_traits" ]
1819

1920
[[test]]
2021
name = "main"

src/macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ macro_rules! __cfg_if_apply {
3535
}
3636

3737
macro_rules! s {
38+
($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
39+
__item! {
40+
#[repr(C)]
41+
$(#[$attr])*
42+
#[cfg_attr(feature = "extra_traits", derive(Eq, PartialEq))]
43+
pub $t $i { $($field)* }
44+
}
45+
impl ::dox::Copy for $i {}
46+
impl ::dox::Clone for $i {
47+
fn clone(&self) -> $i { *self }
48+
}
49+
)*)
50+
}
51+
52+
macro_rules! s_no_extra_traits {
3853
($($(#[$attr:meta])* pub $t:ident $i:ident { $($field:tt)* })*) => ($(
3954
__item! {
4055
#[repr(C)]

src/unix/bsd/apple/b32.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ pub type c_ulong = u32;
55
pub type boolean_t = ::c_int;
66

77
s! {
8-
pub struct pthread_attr_t {
9-
__sig: c_long,
10-
__opaque: [::c_char; 36]
11-
}
12-
138
pub struct if_data {
149
pub ifi_type: ::c_uchar,
1510
pub ifi_typelen: ::c_uchar,
@@ -50,6 +45,26 @@ s! {
5045
}
5146
}
5247

48+
s_no_extra_traits!{
49+
pub struct pthread_attr_t {
50+
__sig: c_long,
51+
__opaque: [::c_char; 36]
52+
}
53+
}
54+
55+
#[cfg(feature = "extra_traits")]
56+
impl PartialEq for pthread_attr_t {
57+
fn eq(&self, other: &pthread_attr_t) -> bool {
58+
self.__sig == other.__sig
59+
&& self.__opaque
60+
.iter()
61+
.zip(other.__opaque.iter())
62+
.all(|(a,b)| a == b)
63+
}
64+
}
65+
#[cfg(feature = "extra_traits")]
66+
impl Eq for pthread_attr_t {}
67+
5368
pub const __PTHREAD_MUTEX_SIZE__: usize = 40;
5469
pub const __PTHREAD_COND_SIZE__: usize = 24;
5570
pub const __PTHREAD_CONDATTR_SIZE__: usize = 4;

src/unix/bsd/apple/b64.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ pub type c_ulong = u64;
55
pub type boolean_t = ::c_uint;
66

77
s! {
8-
pub struct pthread_attr_t {
9-
__sig: c_long,
10-
__opaque: [::c_char; 56]
11-
}
12-
138
pub struct timeval32 {
149
pub tv_sec: i32,
1510
pub tv_usec: i32,
@@ -55,6 +50,26 @@ s! {
5550
}
5651
}
5752

53+
s_no_extra_traits!{
54+
pub struct pthread_attr_t {
55+
__sig: c_long,
56+
__opaque: [::c_char; 56]
57+
}
58+
}
59+
60+
#[cfg(feature = "extra_traits")]
61+
impl PartialEq for pthread_attr_t {
62+
fn eq(&self, other: &pthread_attr_t) -> bool {
63+
self.__sig == other.__sig
64+
&& self.__opaque
65+
.iter()
66+
.zip(other.__opaque.iter())
67+
.all(|(a,b)| a == b)
68+
}
69+
}
70+
#[cfg(feature = "extra_traits")]
71+
impl Eq for pthread_attr_t {}
72+
5873
pub const __PTHREAD_MUTEX_SIZE__: usize = 56;
5974
pub const __PTHREAD_COND_SIZE__: usize = 40;
6075
pub const __PTHREAD_CONDATTR_SIZE__: usize = 8;

0 commit comments

Comments
 (0)