From 6e22f426594cc3f52467483645a2e19a6dd6ef81 Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Fri, 19 Dec 2025 08:34:21 +0800 Subject: [PATCH 1/3] Remove redundancy and fix padding on some bytes display function --- library/core/src/bstr/mod.rs | 14 +------------- library/std/src/sys/os_str/bytes.rs | 21 ++------------------- 2 files changed, 3 insertions(+), 32 deletions(-) diff --git a/library/core/src/bstr/mod.rs b/library/core/src/bstr/mod.rs index 34e1ea66c99ad..bbebfa4b38c15 100644 --- a/library/core/src/bstr/mod.rs +++ b/library/core/src/bstr/mod.rs @@ -155,19 +155,7 @@ unsafe impl DerefPure for ByteStr {} #[unstable(feature = "bstr", issue = "134915")] impl fmt::Debug for ByteStr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "\"")?; - for chunk in self.utf8_chunks() { - for c in chunk.valid().chars() { - match c { - '\0' => write!(f, "\\0")?, - '\x01'..='\x7f' => write!(f, "{}", (c as u8).escape_ascii())?, - _ => write!(f, "{}", c.escape_debug())?, - } - } - write!(f, "{}", chunk.invalid().escape_ascii())?; - } - write!(f, "\"")?; - Ok(()) + fmt::Debug::fmt(&self.utf8_chunks().debug(), f) } } diff --git a/library/std/src/sys/os_str/bytes.rs b/library/std/src/sys/os_str/bytes.rs index 7ba6c46eaef4e..6570095a8a951 100644 --- a/library/std/src/sys/os_str/bytes.rs +++ b/library/std/src/sys/os_str/bytes.rs @@ -1,6 +1,7 @@ //! The underlying OsString/OsStr implementation on Unix and many other //! systems: just a `Vec`/`[u8]`. +use core::bstr::ByteStr; use core::clone::CloneToUninit; use crate::borrow::Cow; @@ -64,25 +65,7 @@ impl fmt::Debug for Slice { impl fmt::Display for Slice { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - // If we're the empty string then our iterator won't actually yield - // anything, so perform the formatting manually - if self.inner.is_empty() { - return "".fmt(f); - } - - for chunk in self.inner.utf8_chunks() { - let valid = chunk.valid(); - // If we successfully decoded the whole chunk as a valid string then - // we can return a direct formatting of the string which will also - // respect various formatting flags if possible. - if chunk.invalid().is_empty() { - return valid.fmt(f); - } - - f.write_str(valid)?; - f.write_char(char::REPLACEMENT_CHARACTER)?; - } - Ok(()) + fmt::Display::fmt(ByteStr::from_bytes(&self.inner), f) } } From a91920bcfd84e955d104d06651b1dae8957fff9e Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Fri, 19 Dec 2025 08:50:39 +0800 Subject: [PATCH 2/3] fix: unused import --- library/std/src/sys/os_str/bytes.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/std/src/sys/os_str/bytes.rs b/library/std/src/sys/os_str/bytes.rs index 6570095a8a951..7028b9dcf39fb 100644 --- a/library/std/src/sys/os_str/bytes.rs +++ b/library/std/src/sys/os_str/bytes.rs @@ -6,7 +6,6 @@ use core::clone::CloneToUninit; use crate::borrow::Cow; use crate::collections::TryReserveError; -use crate::fmt::Write; use crate::rc::Rc; use crate::sync::Arc; use crate::sys::{AsInner, FromInner, IntoInner}; From c5fbb74a0df4524c67acf1de427da1780fbb2ff1 Mon Sep 17 00:00:00 2001 From: yhx-12243 Date: Thu, 22 Jan 2026 10:38:23 +0800 Subject: [PATCH 3/3] Update bytes.rs --- library/std/src/sys/os_str/bytes.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/library/std/src/sys/os_str/bytes.rs b/library/std/src/sys/os_str/bytes.rs index 6074aa33d188f..5482663ef0079 100644 --- a/library/std/src/sys/os_str/bytes.rs +++ b/library/std/src/sys/os_str/bytes.rs @@ -1,7 +1,6 @@ //! The underlying OsString/OsStr implementation on Unix and many other //! systems: just a `Vec`/`[u8]`. -use core::bstr::ByteStr; use core::clone::CloneToUninit; use crate::borrow::Cow;