Skip to content

Commit 1d28750

Browse files
authored
Make the libc "extra_traits" feature optional. (#1049)
Make "libc/extra_traits" optional and enabled by default, so that it can be disabled by --no-default-features. This fixes rustc-dep-of-std builds, because extra_traits depends on std.
1 parent 63dd84a commit 1d28750

File tree

8 files changed

+103
-16
lines changed

8 files changed

+103
-16
lines changed

Cargo.toml

+8-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ once_cell = { version = "1.5.2", optional = true }
3838
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"))))'.dependencies]
3939
linux-raw-sys = { version = "0.4.12", default-features = false, features = ["general", "errno", "ioctl", "no_std", "elf"] }
4040
libc_errno = { package = "errno", version = "0.3.8", default-features = false, optional = true }
41-
libc = { version = "0.2.153", default-features = false, features = ["extra_traits"], optional = true }
41+
libc = { version = "0.2.153", default-features = false, optional = true }
4242

4343
# Dependencies for platforms where only libc is supported:
4444
#
4545
# On all other Unix-family platforms, and under Miri, we always use the libc
4646
# backend, so enable its dependencies unconditionally.
4747
[target.'cfg(all(not(windows), any(rustix_use_libc, miri, not(all(target_os = "linux", target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "riscv64", all(rustix_use_experimental_asm, target_arch = "powerpc64"), all(rustix_use_experimental_asm, target_arch = "mips"), all(rustix_use_experimental_asm, target_arch = "mips32r6"), all(rustix_use_experimental_asm, target_arch = "mips64"), all(rustix_use_experimental_asm, target_arch = "mips64r6"), target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64")))))))'.dependencies]
4848
libc_errno = { package = "errno", version = "0.3.8", default-features = false }
49-
libc = { version = "0.2.153", default-features = false, features = ["extra_traits"] }
49+
libc = { version = "0.2.153", default-features = false }
5050

5151
# Additional dependencies for Linux with the libc backend:
5252
#
@@ -123,10 +123,14 @@ default = ["std", "use-libc-auxv"]
123123

124124
# This enables use of std. Disabling this enables `#![no_std]`, and requires
125125
# Rust 1.64 or newer.
126-
std = ["bitflags/std", "alloc", "libc?/std", "libc_errno?/std"]
126+
std = ["bitflags/std", "alloc", "libc?/std", "libc_errno?/std", "libc-extra-traits"]
127127

128128
# Enable this to request the libc backend.
129-
use-libc = ["libc_errno", "libc"]
129+
use-libc = ["libc_errno", "libc", "libc-extra-traits"]
130+
131+
# Enable `extra_traits` in libc types, to provide `Debug`, `Hash`, and other
132+
# trait impls for libc types.
133+
libc-extra-traits = ["libc?/extra_traits"]
130134

131135
# Enable `rustix::event::*`.
132136
event = []

src/backend/libc/event/poll_fd.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use crate::backend::c;
22
use crate::backend::conv::borrowed_fd;
33
use crate::backend::fd::{AsFd, AsRawFd, BorrowedFd, LibcFd};
4+
#[cfg(windows)]
5+
use crate::backend::fd::{AsSocket, RawFd};
46
use bitflags::bitflags;
7+
use core::fmt;
58
use core::marker::PhantomData;
6-
#[cfg(windows)]
7-
use {
8-
crate::backend::fd::{AsSocket, RawFd},
9-
core::fmt,
10-
};
119

1210
bitflags! {
1311
/// `POLL*` flags for use with [`poll`].
@@ -58,17 +56,15 @@ bitflags! {
5856
/// [`poll`]: crate::event::poll
5957
#[doc(alias = "pollfd")]
6058
#[derive(Clone)]
61-
#[cfg_attr(not(windows), derive(Debug))]
6259
#[repr(transparent)]
6360
pub struct PollFd<'fd> {
6461
pollfd: c::pollfd,
6562
_phantom: PhantomData<BorrowedFd<'fd>>,
6663
}
6764

68-
#[cfg(windows)]
6965
impl<'fd> fmt::Debug for PollFd<'fd> {
7066
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
71-
fmt.debug_struct("pollfd")
67+
fmt.debug_struct("PollFd")
7268
.field("fd", &self.pollfd.fd)
7369
.field("events", &self.pollfd.events)
7470
.field("revents", &self.pollfd.revents)

src/backend/libc/process/cpu_set.rs

+18
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,21 @@ pub(crate) fn CPU_ISSET(cpu: usize, cpuset: &RawCpuSet) -> bool {
4848
pub(crate) fn CPU_COUNT(cpuset: &RawCpuSet) -> u32 {
4949
unsafe { c::CPU_COUNT(cpuset).try_into().unwrap() }
5050
}
51+
52+
#[inline]
53+
pub(crate) fn CPU_EQUAL(this: &RawCpuSet, that: &RawCpuSet) -> bool {
54+
#[cfg(any(linux_like, target_os = "fuchsia", target_os = "hurd"))]
55+
unsafe {
56+
c::CPU_EQUAL(this, that)
57+
}
58+
59+
#[cfg(not(any(linux_like, target_os = "fuchsia", target_os = "hurd")))]
60+
unsafe {
61+
for i in 0..c::CPU_SETSIZE as usize {
62+
if c::CPU_ISSET(i, this) != c::CPU_ISSET(i, that) {
63+
return false;
64+
}
65+
}
66+
true
67+
}
68+
}

src/backend/linux_raw/param/auxv.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ use core::sync::atomic::Ordering::Relaxed;
2222
use core::sync::atomic::{AtomicPtr, AtomicUsize};
2323
use linux_raw_sys::elf::*;
2424
use linux_raw_sys::general::{
25-
AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_MINSIGSTKSZ, AT_NULL, AT_PAGESZ, AT_SYSINFO_EHDR,
25+
AT_BASE, AT_CLKTCK, AT_EXECFN, AT_HWCAP, AT_HWCAP2, AT_MINSIGSTKSZ, AT_NULL, AT_PAGESZ,
26+
AT_SYSINFO_EHDR,
2627
};
2728
#[cfg(feature = "runtime")]
2829
use linux_raw_sys::general::{

src/backend/linux_raw/process/cpu_set.rs

+5
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,8 @@ pub(crate) fn CPU_COUNT_S(size_in_bytes: usize, cpuset: &RawCpuSet) -> u32 {
4444
pub(crate) fn CPU_COUNT(cpuset: &RawCpuSet) -> u32 {
4545
CPU_COUNT_S(core::mem::size_of::<RawCpuSet>(), cpuset)
4646
}
47+
48+
#[inline]
49+
pub(crate) fn CPU_EQUAL(this: &RawCpuSet, that: &RawCpuSet) -> bool {
50+
this.bits == that.bits
51+
}

src/fs/fd.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use backend::fs::types::Stat;
4040
use backend::fs::types::StatFs;
4141
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
4242
use backend::fs::types::StatVfs;
43+
use core::fmt;
4344

4445
/// Timestamps used by [`utimensat`] and [`futimens`].
4546
///
@@ -48,7 +49,7 @@ use backend::fs::types::StatVfs;
4849
// This is `repr(C)` and specifically laid out to match the representation used
4950
// by `utimensat` and `futimens`, which expect 2-element arrays of timestamps.
5051
#[repr(C)]
51-
#[derive(Clone, Debug)]
52+
#[derive(Clone)]
5253
pub struct Timestamps {
5354
/// The timestamp of the last access to a filesystem object.
5455
pub last_access: Timespec,
@@ -57,6 +58,17 @@ pub struct Timestamps {
5758
pub last_modification: Timespec,
5859
}
5960

61+
impl fmt::Debug for Timestamps {
62+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
63+
fmt.debug_struct("Timestamps")
64+
.field("last_access.tv_sec", &self.last_access.tv_sec)
65+
.field("last_access.tv_nsec", &self.last_access.tv_nsec)
66+
.field("last_modification.tv_sec", &self.last_modification.tv_sec)
67+
.field("last_modification.tv_nsec", &self.last_modification.tv_nsec)
68+
.finish()
69+
}
70+
}
71+
6072
/// The filesystem magic number for procfs.
6173
///
6274
/// See [the `fstatfs` manual page] for more information.

src/process/sched.rs

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::process::Pid;
22
use crate::{backend, io};
3+
use core::{fmt, hash};
34

45
/// `CpuSet` represents a bit-mask of CPUs.
56
///
@@ -13,7 +14,7 @@ use crate::{backend, io};
1314
/// [`sched_setaffinity`]: crate::process::sched_setaffinity
1415
/// [`sched_getaffinity`]: crate::process::sched_getaffinity
1516
#[repr(C)]
16-
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
17+
#[derive(Clone, Copy)]
1718
pub struct CpuSet {
1819
cpu_set: backend::process::types::RawCpuSet,
1920
}
@@ -75,6 +76,41 @@ impl Default for CpuSet {
7576
}
7677
}
7778

79+
impl fmt::Debug for CpuSet {
80+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
81+
write!(fmt, "CpuSet {{")?;
82+
let mut first = true;
83+
for i in 0..CpuSet::MAX_CPU {
84+
if self.is_set(i) {
85+
if first {
86+
write!(fmt, " ")?;
87+
first = false;
88+
} else {
89+
write!(fmt, ", ")?;
90+
}
91+
write!(fmt, "cpu{}", i)?;
92+
}
93+
}
94+
write!(fmt, " }}")
95+
}
96+
}
97+
98+
impl hash::Hash for CpuSet {
99+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
100+
for i in 0..CpuSet::MAX_CPU {
101+
self.is_set(i).hash(state);
102+
}
103+
}
104+
}
105+
106+
impl Eq for CpuSet {}
107+
108+
impl PartialEq for CpuSet {
109+
fn eq(&self, other: &Self) -> bool {
110+
backend::process::cpu_set::CPU_EQUAL(&self.cpu_set, &other.cpu_set)
111+
}
112+
}
113+
78114
/// `sched_setaffinity(pid, cpuset)`—Set a thread's CPU affinity mask.
79115
///
80116
/// `pid` is the thread ID to update. If pid is `None`, then the current thread

src/thread/clock.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::{backend, io};
2+
use core::fmt;
23

34
pub use crate::timespec::Timespec;
45

@@ -88,7 +89,7 @@ pub fn nanosleep(request: &Timespec) -> NanosleepRelativeResult {
8889
}
8990

9091
/// A return type for `nanosleep` and `clock_nanosleep_relative`.
91-
#[derive(Debug, Clone)]
92+
#[derive(Clone)]
9293
#[must_use]
9394
pub enum NanosleepRelativeResult {
9495
/// The sleep completed normally.
@@ -98,3 +99,17 @@ pub enum NanosleepRelativeResult {
9899
/// An invalid time value was provided.
99100
Err(io::Errno),
100101
}
102+
103+
impl fmt::Debug for NanosleepRelativeResult {
104+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
105+
match self {
106+
NanosleepRelativeResult::Ok => fmt.write_str("Ok"),
107+
NanosleepRelativeResult::Interrupted(remaining) => write!(
108+
fmt,
109+
"Interrupted(Timespec {{ tv_sec: {:?}, tv_nsec: {:?} }})",
110+
remaining.tv_sec, remaining.tv_nsec
111+
),
112+
NanosleepRelativeResult::Err(err) => write!(fmt, "Err({:?})", err),
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)