Skip to content

Commit 1ae22d8

Browse files
committed
Implement Debug for EncodeWide
Since `std::os::windows::ffi::EncodeWide` was reexported from `std::sys_common::wtf8::EncodeWide`, which has `#![allow(missing_debug_implementations)]` in the parent module, it did not implement `Debug`. When it was moved to `core`, a placeholder impl was added; fill it in.
1 parent eec6bd9 commit 1ae22d8

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

library/alloc/src/wtf8/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ fn wtf8_encode_wide_size_hint() {
579579
assert!(iter.next().is_none());
580580
}
581581

582+
#[test]
583+
fn wtf8_encode_wide_debug() {
584+
let mut string = Wtf8Buf::from_str("aé ");
585+
string.push(CodePoint::from_u32(0xD83D).unwrap());
586+
string.push_char('💩');
587+
assert_eq!(
588+
format!("{:?}", string.encode_wide()),
589+
r#"EncodeWide(['a', 'é', ' ', 0xD83D, 0xD83D, 0xDCA9])"#
590+
);
591+
}
592+
582593
#[test]
583594
fn wtf8_clone_into() {
584595
let mut string = Wtf8Buf::new();

library/core/src/wtf8.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,36 @@ impl Iterator for EncodeWide<'_> {
562562
}
563563
}
564564

565+
#[stable(feature = "encode_wide_fused_iterator", since = "1.62.0")]
566+
impl FusedIterator for EncodeWide<'_> {}
567+
568+
#[stable(feature = "encode_wide_debug", since = "CURRENT_RUSTC_VERSION")]
565569
impl fmt::Debug for EncodeWide<'_> {
566570
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
567-
f.debug_struct("EncodeWide").finish_non_exhaustive()
571+
struct CodeUnit(u16);
572+
impl fmt::Debug for CodeUnit {
573+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
574+
// This output attempts to balance readability with precision.
575+
// Render characters which take only one WTF-16 code unit using
576+
// `char` syntax and everything else as code units with hex
577+
// integer syntax (including paired and unpaired surrogate
578+
// halves). Since Rust has no `char`-like type for WTF-16, this
579+
// isn't perfect, so if this output isn't suitable, it is open
580+
// to being changed (see #140153).
581+
match char::from_u32(self.0 as u32) {
582+
Some(c) => write!(f, "{c:?}"),
583+
None => write!(f, "0x{:04X}", self.0),
584+
}
585+
}
586+
}
587+
588+
write!(f, "EncodeWide(")?;
589+
f.debug_list().entries(self.clone().map(CodeUnit)).finish()?;
590+
write!(f, ")")?;
591+
Ok(())
568592
}
569593
}
570594

571-
#[stable(feature = "encode_wide_fused_iterator", since = "1.62.0")]
572-
impl FusedIterator for EncodeWide<'_> {}
573-
574595
impl Hash for CodePoint {
575596
#[inline]
576597
fn hash<H: Hasher>(&self, state: &mut H) {

library/std/src/sys_common/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//! Progress on this is tracked in #84187.
1616
1717
#![allow(missing_docs)]
18-
#![allow(missing_debug_implementations)]
1918

2019
#[cfg(test)]
2120
mod tests;

0 commit comments

Comments
 (0)