Skip to content

Commit d211853

Browse files
committed
Implement Debug for EncodeWide
Since `std::os::windows::ffi::EncodeWide` is reexported from `std::sys_common::wtf8::EncodeWide`, which has `#![allow(missing_debug_implementations)]` in the parent module, it does not implement `Debug`. Implement it akin to `core::str::Chars`. The only question is how to format each WTF-16 code unit. We can't format it like `char`, because \u escape sequences for surrogate halves are invalid syntax in Rust. It would be nice to format them as hex integers, but I am not aware of other instances of that pattern for `Debug` in `std`. Settle for decimal.
1 parent 9bfa31f commit d211853

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

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;

library/std/src/sys_common/wtf8.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,16 @@ impl Iterator for EncodeWide<'_> {
10491049
#[stable(feature = "encode_wide_fused_iterator", since = "1.62.0")]
10501050
impl FusedIterator for EncodeWide<'_> {}
10511051

1052+
#[stable(feature = "encode_wide_debug", since = "CURRENT_RUSTC_VERSION")]
1053+
impl fmt::Debug for EncodeWide<'_> {
1054+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1055+
write!(f, "EncodeWide(")?;
1056+
f.debug_list().entries(self.clone()).finish()?;
1057+
write!(f, ")")?;
1058+
Ok(())
1059+
}
1060+
}
1061+
10521062
impl Hash for CodePoint {
10531063
#[inline]
10541064
fn hash<H: Hasher>(&self, state: &mut H) {

library/std/src/sys_common/wtf8/tests.rs

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

577+
#[test]
578+
fn wtf8_encode_wide_debug() {
579+
let mut string = Wtf8Buf::from_str("aé ");
580+
string.push(CodePoint::from_u32(0xD83D).unwrap());
581+
string.push_char('💩');
582+
assert_eq!(
583+
format!("{:?}", string.encode_wide()),
584+
r#"EncodeWide([97, 233, 32, 55357, 55357, 56489])"#
585+
);
586+
}
587+
577588
#[test]
578589
fn wtf8_clone_into() {
579590
let mut string = Wtf8Buf::new();

0 commit comments

Comments
 (0)