Skip to content

Commit c100954

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. (backport <#4806>) (cherry picked from commit 141aea1) [ note that this was very conflict-heavy so the cherry pick differs quite a bit from the original commit - Trevor ]
1 parent 5e113a3 commit c100954

File tree

54 files changed

+561
-6733
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

+561
-6733
lines changed

src/fuchsia/mod.rs

Lines changed: 2 additions & 265 deletions
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,7 @@ s! {
880880
pub struct pthread_condattr_t {
881881
size: [u8; crate::__SIZEOF_PTHREAD_CONDATTR_T],
882882
}
883-
}
884883

885-
s_no_extra_traits! {
886884
pub struct sysinfo {
887885
pub uptime: c_ulong,
888886
pub loads: [c_ulong; 3],
@@ -969,6 +967,8 @@ s_no_extra_traits! {
969967
pub nl_groups: u32,
970968
}
971969

970+
// FIXME(msrv): suggested method was added in 1.85
971+
#[allow(unpredictable_function_pointer_comparisons)]
972972
pub struct sigevent {
973973
pub sigev_value: crate::sigval,
974974
pub sigev_signo: c_int,
@@ -1023,269 +1023,6 @@ s_no_extra_traits! {
10231023
}
10241024
}
10251025

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

12911028
pub const INT_MIN: c_int = -2147483648;

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;

src/unix/aix/mod.rs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,6 @@ s! {
544544
pub sa_mask: sigset_t,
545545
pub sa_flags: c_int,
546546
}
547-
}
548-
549-
s_no_extra_traits! {
550-
pub union __poll_ctl_ext_u {
551-
pub addr: *mut c_void,
552-
pub data32: u32,
553-
pub data: u64,
554-
}
555547

556548
pub struct poll_ctl_ext {
557549
pub version: u8,
@@ -563,6 +555,14 @@ s_no_extra_traits! {
563555
}
564556
}
565557

558+
s_no_extra_traits! {
559+
pub union __poll_ctl_ext_u {
560+
pub addr: *mut c_void,
561+
pub data32: u32,
562+
pub data: u64,
563+
}
564+
}
565+
566566
cfg_if! {
567567
if #[cfg(feature = "extra_traits")] {
568568
impl PartialEq for __poll_ctl_ext_u {
@@ -584,28 +584,6 @@ cfg_if! {
584584
}
585585
}
586586
}
587-
588-
impl PartialEq for poll_ctl_ext {
589-
fn eq(&self, other: &poll_ctl_ext) -> bool {
590-
self.version == other.version
591-
&& self.command == other.command
592-
&& self.events == other.events
593-
&& self.fd == other.fd
594-
&& self.reserved64 == other.reserved64
595-
&& self.u == other.u
596-
}
597-
}
598-
impl Eq for poll_ctl_ext {}
599-
impl hash::Hash for poll_ctl_ext {
600-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
601-
self.version.hash(state);
602-
self.command.hash(state);
603-
self.events.hash(state);
604-
self.fd.hash(state);
605-
self.u.hash(state);
606-
self.reserved64.hash(state);
607-
}
608-
}
609587
}
610588
}
611589

0 commit comments

Comments
 (0)