From dd2fe86ac682575e7f393d40ece452ba954a5703 Mon Sep 17 00:00:00 2001 From: rusty-snake <41237666+rusty-snake@users.noreply.github.com> Date: Sat, 12 Apr 2025 00:14:47 +0200 Subject: [PATCH] Implement more formatter for Pid, Uid, Gid Closes #1439 Co-authored-by: Tom Schuster --- src/pid.rs | 38 ++++++++++++++++++++++++++- src/ugid.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-) diff --git a/src/pid.rs b/src/pid.rs index d09cba0e3..d82ee0756 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -2,7 +2,7 @@ #![allow(unsafe_code)] -use core::num::NonZeroI32; +use core::{fmt, num::NonZeroI32}; /// A process identifier as a raw integer. pub type RawPid = i32; @@ -94,6 +94,42 @@ impl Pid { } } +impl fmt::Display for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::Binary for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::Octal for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::LowerHex for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::UpperHex for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::LowerExp for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::UpperExp for Pid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ugid.rs b/src/ugid.rs index 386ceb72d..11edb19b3 100644 --- a/src/ugid.rs +++ b/src/ugid.rs @@ -1,5 +1,7 @@ //! User and Group ID types. +use core::fmt; + use crate::backend::c; use crate::ffi; @@ -86,6 +88,78 @@ impl Gid { } } +impl fmt::Display for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::Binary for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::Octal for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::LowerHex for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::UpperHex for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::LowerExp for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::UpperExp for Uid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Display for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::Binary for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::Octal for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::LowerHex for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::UpperHex for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::LowerExp for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} +impl fmt::UpperExp for Gid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + // Return the raw value of the IDs. In case of `None` it returns `!0` since it // has the same bit pattern as `-1` indicating no change to the owner/group ID. pub(crate) fn translate_fchown_args(