Skip to content

Commit 141aea1

Browse files
committed
cleanup: Use derived traits as much as possible
Historically Rust hasn't supported derives on packed structs well, and until recently our automatic derives via `s!` still did not support these (fixed by 79d1bdb ("Flip `Copy` and `PartialEq, Hash` derives")). This meant that any packed struct implementing `extra_traits` had handwritten implementations that are no longer needed. There are also a handful of cases that use a handwritten implementation in order to selectively exclude padding fields. Others don't seem to be handwritten for any specific reason. Clean up all of the above by switching from `s_no_extra_traits!` to `s!` and deleting the handwritten implementations wherever possible. The only remaining handwritten implementations are those that compare unions with `unsafe` (which will have to be addressed) or floats. Note that in some cases, this means we are no longer excluding padding fields from comparisons. These traits are only usable via references, not raw pointers; thus, the user has asserted that they are initialized, and there is no UB in `libc` by reading them. Practically the only way to know they are initialized is by starting with a zeroed struct, so in most cases they will have no effect on the results. Whether or not padding fields are included in handwritten traits is also completely inconsistent (most include them), so there isn't much predictability lost here. The new `Padding` type will also improve the situation here in the future.
1 parent 79d1bdb commit 141aea1

File tree

54 files changed

+592
-6769
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+592
-6769
lines changed

src/fuchsia/mod.rs

Lines changed: 4 additions & 261 deletions
Original file line numberDiff line numberDiff line change
@@ -875,9 +875,7 @@ s! {
875875
pub struct pthread_condattr_t {
876876
size: [u8; crate::__SIZEOF_PTHREAD_CONDATTR_T],
877877
}
878-
}
879878

880-
s_no_extra_traits! {
881879
pub struct sysinfo {
882880
pub uptime: c_ulong,
883881
pub loads: [c_ulong; 3],
@@ -964,6 +962,8 @@ s_no_extra_traits! {
964962
pub nl_groups: u32,
965963
}
966964

965+
// FIXME(msrv): suggested method was added in 1.85
966+
#[allow(unpredictable_function_pointer_comparisons)]
967967
pub struct sigevent {
968968
pub sigev_value: crate::sigval,
969969
pub sigev_signo: c_int,
@@ -1016,7 +1016,9 @@ s_no_extra_traits! {
10161016
pub struct pthread_cond_t {
10171017
size: [u8; crate::__SIZEOF_PTHREAD_COND_T],
10181018
}
1019+
}
10191020

1021+
s_no_extra_traits! {
10201022
pub union sigval {
10211023
pub sival_int: c_int,
10221024
pub sival_ptr: *mut c_void,
@@ -1037,265 +1039,6 @@ s_no_extra_traits! {
10371039

10381040
cfg_if! {
10391041
if #[cfg(feature = "extra_traits")] {
1040-
impl PartialEq for sysinfo {
1041-
fn eq(&self, other: &sysinfo) -> bool {
1042-
self.uptime == other.uptime
1043-
&& self.loads == other.loads
1044-
&& self.totalram == other.totalram
1045-
&& self.freeram == other.freeram
1046-
&& self.sharedram == other.sharedram
1047-
&& self.bufferram == other.bufferram
1048-
&& self.totalswap == other.totalswap
1049-
&& self.freeswap == other.freeswap
1050-
&& self.procs == other.procs
1051-
&& self.pad == other.pad
1052-
&& self.totalhigh == other.totalhigh
1053-
&& self.freehigh == other.freehigh
1054-
&& self.mem_unit == other.mem_unit
1055-
&& self
1056-
.__reserved
1057-
.iter()
1058-
.zip(other.__reserved.iter())
1059-
.all(|(a, b)| a == b)
1060-
}
1061-
}
1062-
impl Eq for sysinfo {}
1063-
impl hash::Hash for sysinfo {
1064-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1065-
self.uptime.hash(state);
1066-
self.loads.hash(state);
1067-
self.totalram.hash(state);
1068-
self.freeram.hash(state);
1069-
self.sharedram.hash(state);
1070-
self.bufferram.hash(state);
1071-
self.totalswap.hash(state);
1072-
self.freeswap.hash(state);
1073-
self.procs.hash(state);
1074-
self.pad.hash(state);
1075-
self.totalhigh.hash(state);
1076-
self.freehigh.hash(state);
1077-
self.mem_unit.hash(state);
1078-
self.__reserved.hash(state);
1079-
}
1080-
}
1081-
1082-
impl PartialEq for sockaddr_un {
1083-
fn eq(&self, other: &sockaddr_un) -> bool {
1084-
self.sun_family == other.sun_family
1085-
&& self
1086-
.sun_path
1087-
.iter()
1088-
.zip(other.sun_path.iter())
1089-
.all(|(a, b)| a == b)
1090-
}
1091-
}
1092-
impl Eq for sockaddr_un {}
1093-
impl hash::Hash for sockaddr_un {
1094-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1095-
self.sun_family.hash(state);
1096-
self.sun_path.hash(state);
1097-
}
1098-
}
1099-
1100-
impl PartialEq for sockaddr_storage {
1101-
fn eq(&self, other: &sockaddr_storage) -> bool {
1102-
self.ss_family == other.ss_family
1103-
&& self.__ss_align == other.__ss_align
1104-
&& self
1105-
.__ss_pad2
1106-
.iter()
1107-
.zip(other.__ss_pad2.iter())
1108-
.all(|(a, b)| a == b)
1109-
}
1110-
}
1111-
impl Eq for sockaddr_storage {}
1112-
impl hash::Hash for sockaddr_storage {
1113-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1114-
self.ss_family.hash(state);
1115-
self.__ss_align.hash(state);
1116-
self.__ss_pad2.hash(state);
1117-
}
1118-
}
1119-
1120-
impl PartialEq for utsname {
1121-
fn eq(&self, other: &utsname) -> bool {
1122-
self.sysname
1123-
.iter()
1124-
.zip(other.sysname.iter())
1125-
.all(|(a, b)| a == b)
1126-
&& self
1127-
.nodename
1128-
.iter()
1129-
.zip(other.nodename.iter())
1130-
.all(|(a, b)| a == b)
1131-
&& self
1132-
.release
1133-
.iter()
1134-
.zip(other.release.iter())
1135-
.all(|(a, b)| a == b)
1136-
&& self
1137-
.version
1138-
.iter()
1139-
.zip(other.version.iter())
1140-
.all(|(a, b)| a == b)
1141-
&& self
1142-
.machine
1143-
.iter()
1144-
.zip(other.machine.iter())
1145-
.all(|(a, b)| a == b)
1146-
}
1147-
}
1148-
impl Eq for utsname {}
1149-
impl hash::Hash for utsname {
1150-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1151-
self.sysname.hash(state);
1152-
self.nodename.hash(state);
1153-
self.release.hash(state);
1154-
self.version.hash(state);
1155-
self.machine.hash(state);
1156-
}
1157-
}
1158-
1159-
impl PartialEq for dirent {
1160-
fn eq(&self, other: &dirent) -> bool {
1161-
self.d_ino == other.d_ino
1162-
&& self.d_off == other.d_off
1163-
&& self.d_reclen == other.d_reclen
1164-
&& self.d_type == other.d_type
1165-
&& self
1166-
.d_name
1167-
.iter()
1168-
.zip(other.d_name.iter())
1169-
.all(|(a, b)| a == b)
1170-
}
1171-
}
1172-
impl Eq for dirent {}
1173-
impl hash::Hash for dirent {
1174-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1175-
self.d_ino.hash(state);
1176-
self.d_off.hash(state);
1177-
self.d_reclen.hash(state);
1178-
self.d_type.hash(state);
1179-
self.d_name.hash(state);
1180-
}
1181-
}
1182-
1183-
impl PartialEq for dirent64 {
1184-
fn eq(&self, other: &dirent64) -> bool {
1185-
self.d_ino == other.d_ino
1186-
&& self.d_off == other.d_off
1187-
&& self.d_reclen == other.d_reclen
1188-
&& self.d_type == other.d_type
1189-
&& self
1190-
.d_name
1191-
.iter()
1192-
.zip(other.d_name.iter())
1193-
.all(|(a, b)| a == b)
1194-
}
1195-
}
1196-
impl Eq for dirent64 {}
1197-
impl hash::Hash for dirent64 {
1198-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1199-
self.d_ino.hash(state);
1200-
self.d_off.hash(state);
1201-
self.d_reclen.hash(state);
1202-
self.d_type.hash(state);
1203-
self.d_name.hash(state);
1204-
}
1205-
}
1206-
1207-
impl PartialEq for mq_attr {
1208-
fn eq(&self, other: &mq_attr) -> bool {
1209-
self.mq_flags == other.mq_flags
1210-
&& self.mq_maxmsg == other.mq_maxmsg
1211-
&& self.mq_msgsize == other.mq_msgsize
1212-
&& self.mq_curmsgs == other.mq_curmsgs
1213-
}
1214-
}
1215-
impl Eq for mq_attr {}
1216-
impl hash::Hash for mq_attr {
1217-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1218-
self.mq_flags.hash(state);
1219-
self.mq_maxmsg.hash(state);
1220-
self.mq_msgsize.hash(state);
1221-
self.mq_curmsgs.hash(state);
1222-
}
1223-
}
1224-
1225-
impl PartialEq for sockaddr_nl {
1226-
fn eq(&self, other: &sockaddr_nl) -> bool {
1227-
self.nl_family == other.nl_family
1228-
&& self.nl_pid == other.nl_pid
1229-
&& self.nl_groups == other.nl_groups
1230-
}
1231-
}
1232-
impl Eq for sockaddr_nl {}
1233-
impl hash::Hash for sockaddr_nl {
1234-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1235-
self.nl_family.hash(state);
1236-
self.nl_pid.hash(state);
1237-
self.nl_groups.hash(state);
1238-
}
1239-
}
1240-
1241-
// FIXME(msrv): suggested method was added in 1.85
1242-
#[allow(unpredictable_function_pointer_comparisons)]
1243-
impl PartialEq for sigevent {
1244-
fn eq(&self, other: &sigevent) -> bool {
1245-
self.sigev_value == other.sigev_value
1246-
&& self.sigev_signo == other.sigev_signo
1247-
&& self.sigev_notify == other.sigev_notify
1248-
&& self.sigev_notify_function == other.sigev_notify_function
1249-
&& self.sigev_notify_attributes == other.sigev_notify_attributes
1250-
}
1251-
}
1252-
impl Eq for sigevent {}
1253-
impl hash::Hash for sigevent {
1254-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1255-
self.sigev_value.hash(state);
1256-
self.sigev_signo.hash(state);
1257-
self.sigev_notify.hash(state);
1258-
self.sigev_notify_function.hash(state);
1259-
self.sigev_notify_attributes.hash(state);
1260-
}
1261-
}
1262-
1263-
impl PartialEq for pthread_cond_t {
1264-
fn eq(&self, other: &pthread_cond_t) -> bool {
1265-
self.size.iter().zip(other.size.iter()).all(|(a, b)| a == b)
1266-
}
1267-
}
1268-
impl Eq for pthread_cond_t {}
1269-
impl hash::Hash for pthread_cond_t {
1270-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1271-
self.size.hash(state);
1272-
}
1273-
}
1274-
1275-
impl PartialEq for pthread_mutex_t {
1276-
fn eq(&self, other: &pthread_mutex_t) -> bool {
1277-
self.size.iter().zip(other.size.iter()).all(|(a, b)| a == b)
1278-
}
1279-
}
1280-
impl Eq for pthread_mutex_t {}
1281-
impl hash::Hash for pthread_mutex_t {
1282-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1283-
self.size.hash(state);
1284-
}
1285-
}
1286-
1287-
impl PartialEq for pthread_rwlock_t {
1288-
fn eq(&self, other: &pthread_rwlock_t) -> bool {
1289-
self.size.iter().zip(other.size.iter()).all(|(a, b)| a == b)
1290-
}
1291-
}
1292-
impl Eq for pthread_rwlock_t {}
1293-
impl hash::Hash for pthread_rwlock_t {
1294-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
1295-
self.size.hash(state);
1296-
}
1297-
}
1298-
12991042
impl PartialEq for sigval {
13001043
fn eq(&self, _other: &sigval) -> bool {
13011044
unimplemented!("traits")

src/fuchsia/x86_64.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ s! {
6464
__unused1: c_long,
6565
__unused2: c_long,
6666
}
67-
}
6867

69-
s_no_extra_traits! {
7068
pub struct ucontext_t {
7169
pub uc_flags: c_ulong,
7270
pub uc_link: *mut ucontext_t,
@@ -77,36 +75,6 @@ s_no_extra_traits! {
7775
}
7876
}
7977

80-
cfg_if! {
81-
if #[cfg(feature = "extra_traits")] {
82-
impl PartialEq for ucontext_t {
83-
fn eq(&self, other: &ucontext_t) -> bool {
84-
self.uc_flags == other.uc_flags
85-
&& self.uc_link == other.uc_link
86-
&& self.uc_stack == other.uc_stack
87-
&& self.uc_mcontext == other.uc_mcontext
88-
&& self.uc_sigmask == other.uc_sigmask
89-
&& self
90-
.__private
91-
.iter()
92-
.zip(other.__private.iter())
93-
.all(|(a, b)| a == b)
94-
}
95-
}
96-
impl Eq for ucontext_t {}
97-
impl hash::Hash for ucontext_t {
98-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
99-
self.uc_flags.hash(state);
100-
self.uc_link.hash(state);
101-
self.uc_stack.hash(state);
102-
self.uc_mcontext.hash(state);
103-
self.uc_sigmask.hash(state);
104-
self.__private.hash(state);
105-
}
106-
}
107-
}
108-
}
109-
11078
// offsets in user_regs_structs, from sys/reg.h
11179
pub const R15: c_int = 0;
11280
pub const R14: c_int = 1;

0 commit comments

Comments
 (0)