Skip to content

Commit 3602b47

Browse files
authored
Clean up Debug impls. (#1360)
`Timespec` implements `Debug` now, so we no longer need workarounds for it not doing so. And, using `core::fmt`, `Debug` impls don't depend on `feature = "std"`.
1 parent 5ee1ab8 commit 3602b47

6 files changed

Lines changed: 14 additions & 62 deletions

File tree

examples/time.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,45 @@
11
//! A command which prints the current values of the realtime and monotonic
22
//! clocks it's given.
33
4-
#[cfg(not(any(windows, target_os = "espidf")))]
5-
#[cfg(feature = "time")]
6-
struct DebugTimespec(rustix::time::Timespec);
7-
8-
#[cfg(not(any(windows, target_os = "espidf")))]
9-
#[cfg(feature = "time")]
10-
impl core::fmt::Debug for DebugTimespec {
11-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
12-
let mut d = f.debug_struct("Timespec");
13-
d.field("tv_sec", &self.0.tv_sec);
14-
d.field("tv_nsec", &self.0.tv_nsec);
15-
d.finish()
16-
}
17-
}
18-
194
#[cfg(not(any(windows, target_os = "espidf")))]
205
#[cfg(feature = "time")]
216
fn main() {
227
use rustix::time::{clock_gettime, ClockId};
238

24-
println!(
25-
"Real time: {:?}",
26-
DebugTimespec(clock_gettime(ClockId::Realtime))
27-
);
28-
println!(
29-
"Monotonic time: {:?}",
30-
DebugTimespec(clock_gettime(ClockId::Monotonic))
31-
);
9+
println!("Real time: {:?}", clock_gettime(ClockId::Realtime));
10+
println!("Monotonic time: {:?}", clock_gettime(ClockId::Monotonic));
3211

3312
#[cfg(any(freebsdlike, target_os = "openbsd"))]
3413
println!("Uptime: {:?}", clock_gettime(ClockId::Uptime));
3514

3615
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
3716
println!(
3817
"Process CPU time: {:?}",
39-
DebugTimespec(clock_gettime(ClockId::ProcessCPUTime))
18+
clock_gettime(ClockId::ProcessCPUTime)
4019
);
4120

4221
#[cfg(not(any(solarish, target_os = "netbsd", target_os = "redox")))]
4322
println!(
4423
"Thread CPU time: {:?}",
45-
DebugTimespec(clock_gettime(ClockId::ThreadCPUTime))
24+
clock_gettime(ClockId::ThreadCPUTime)
4625
);
4726

4827
#[cfg(any(linux_kernel, target_os = "freebsd"))]
4928
println!(
5029
"Realtime (coarse): {:?}",
51-
DebugTimespec(clock_gettime(ClockId::RealtimeCoarse))
30+
clock_gettime(ClockId::RealtimeCoarse)
5231
);
5332

5433
#[cfg(any(linux_kernel, target_os = "freebsd"))]
5534
println!(
5635
"Monotonic (coarse): {:?}",
57-
DebugTimespec(clock_gettime(ClockId::MonotonicCoarse))
36+
clock_gettime(ClockId::MonotonicCoarse)
5837
);
5938

6039
#[cfg(linux_kernel)]
6140
println!(
6241
"Monotonic (raw): {:?}",
63-
DebugTimespec(clock_gettime(ClockId::MonotonicRaw))
42+
clock_gettime(ClockId::MonotonicRaw)
6443
);
6544
}
6645

src/fs/fd.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use backend::fs::types::Stat;
4242
use backend::fs::types::StatFs;
4343
#[cfg(not(any(target_os = "haiku", target_os = "redox", target_os = "wasi")))]
4444
use backend::fs::types::StatVfs;
45-
use core::fmt;
4645

4746
/// Timestamps used by [`utimensat`] and [`futimens`].
4847
///
@@ -51,7 +50,7 @@ use core::fmt;
5150
// This is `repr(C)` and specifically laid out to match the representation used
5251
// by `utimensat` and `futimens`, which expect 2-element arrays of timestamps.
5352
#[repr(C)]
54-
#[derive(Clone)]
53+
#[derive(Debug, Clone)]
5554
pub struct Timestamps {
5655
/// The timestamp of the last access to a filesystem object.
5756
pub last_access: Timespec,
@@ -60,17 +59,6 @@ pub struct Timestamps {
6059
pub last_modification: Timespec,
6160
}
6261

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

src/fs/raw_dir.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
//! `RawDir` and `RawDirEntry`.
22
3-
use core::fmt;
4-
use core::mem::{align_of, MaybeUninit};
5-
use linux_raw_sys::general::linux_dirent64;
6-
73
use crate::backend::fs::syscalls::getdents_uninit;
84
use crate::fd::AsFd;
95
use crate::ffi::CStr;
106
use crate::fs::FileType;
117
use crate::io;
8+
use core::fmt;
9+
use core::mem::{align_of, MaybeUninit};
10+
use linux_raw_sys::general::linux_dirent64;
1211

1312
/// A directory iterator implemented with getdents.
1413
///

src/net/send_recv/msg.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use crate::io::{self, IoSlice, IoSliceMut};
1010
use crate::net::addr::SocketAddrArg;
1111
#[cfg(linux_kernel)]
1212
use crate::net::UCred;
13-
#[cfg(feature = "std")]
14-
use core::fmt;
1513
use core::iter::FusedIterator;
1614
use core::marker::PhantomData;
1715
use core::mem::{align_of, size_of, size_of_val, take, MaybeUninit};
@@ -771,6 +769,7 @@ pub fn recvmsg<Fd: AsFd>(
771769
}
772770

773771
/// The result of a successful [`recvmsg`] call.
772+
#[derive(Debug, Clone)]
774773
pub struct RecvMsg {
775774
/// The number of bytes received.
776775
///
@@ -786,17 +785,6 @@ pub struct RecvMsg {
786785
pub address: Option<SocketAddrAny>,
787786
}
788787

789-
#[cfg(feature = "std")]
790-
impl fmt::Debug for RecvMsg {
791-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
792-
f.debug_struct("RecvMsg")
793-
.field("bytes", &self.bytes)
794-
.field("flags", &self.flags)
795-
.field("address", &self.address)
796-
.finish()
797-
}
798-
}
799-
800788
/// An iterator over data in an ancillary buffer.
801789
pub struct AncillaryIter<'data, T> {
802790
/// The data we're iterating over.

src/net/socket_addr_any.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::net::addr::{SocketAddrArg, SocketAddrLen, SocketAddrOpaque, SocketAdd
99
#[cfg(unix)]
1010
use crate::net::SocketAddrUnix;
1111
use crate::net::{AddressFamily, SocketAddr, SocketAddrV4, SocketAddrV6};
12-
#[cfg(feature = "std")]
1312
use core::fmt;
1413
use core::mem::{size_of, MaybeUninit};
1514
use core::num::NonZeroU32;
@@ -195,7 +194,6 @@ impl core::hash::Hash for SocketAddrAny {
195194
}
196195
}
197196

198-
#[cfg(feature = "std")]
199197
impl fmt::Debug for SocketAddrAny {
200198
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201199
match self.address_family() {

src/path/dec_int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use crate::backend::fd::{AsFd, AsRawFd as _};
1010
use crate::ffi::CStr;
11+
use core::fmt;
1112
use core::hint::unreachable_unchecked;
1213
use core::mem::{self, MaybeUninit};
1314
use core::num::{NonZeroU8, NonZeroUsize};
@@ -20,7 +21,7 @@ use std::os::unix::ffi::OsStrExt;
2021
))]
2122
use std::os::wasi::ffi::OsStrExt;
2223
#[cfg(feature = "std")]
23-
use {core::fmt, std::ffi::OsStr, std::path::Path};
24+
use {std::ffi::OsStr, std::path::Path};
2425

2526
/// Format an integer into a decimal `Path` component, without constructing a
2627
/// temporary `PathBuf` or `String`.
@@ -253,7 +254,6 @@ impl AsRef<Path> for DecInt {
253254
}
254255
}
255256

256-
#[cfg(feature = "std")]
257257
impl fmt::Debug for DecInt {
258258
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259259
self.as_str().fmt(f)

0 commit comments

Comments
 (0)