Skip to content

Commit be62715

Browse files
authored
Merge pull request #32 from artichoke/fmt-slice-debug
Simplify debug reprs of Slice internals
2 parents 8d9292e + 156d6b4 commit be62715

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/internal.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,22 @@ where
8787

8888
impl fmt::Debug for Interned<str> {
8989
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90-
write!(f, "{:?}", self.0)
90+
if f.alternate() {
91+
write!(f, "{:#?}", self.0)
92+
} else {
93+
write!(f, "{:?}", self.0)
94+
}
9195
}
9296
}
9397

9498
#[cfg(feature = "bytes")]
9599
impl fmt::Debug for Interned<[u8]> {
96100
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97-
write!(f, "{:?}", self.0)
101+
if f.alternate() {
102+
write!(f, "{:#?}", self.0)
103+
} else {
104+
write!(f, "{:?}", self.0)
105+
}
98106
}
99107
}
100108

@@ -307,20 +315,24 @@ where
307315
}
308316

309317
impl fmt::Debug for Slice<str> {
318+
/// Formats the string slice using the given formatter.
310319
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
311-
match self {
312-
Self::Static(_) => write!(f, "Static({:?})", self),
313-
Self::Owned(_) => write!(f, "Owned({:?})", self),
314-
}
320+
write!(f, "{:?}", self.as_slice())
315321
}
316322
}
317323

318324
#[cfg(feature = "bytes")]
319325
impl fmt::Debug for Slice<[u8]> {
326+
/// Formats the byte slice using the given formatter.
327+
///
328+
/// If alternate format is specified, e.g. `{:#?}`, the slice is assumed to
329+
/// be conventionally UTF-8 and converted to a [`String`] lossily with
330+
/// [`String::from_utf8_lossy`].
320331
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
321-
match self {
322-
Self::Static(_) => write!(f, "Static({:?})", String::from_utf8_lossy(self.as_slice())),
323-
Self::Owned(_) => write!(f, "Owned({:?})", String::from_utf8_lossy(self.as_slice())),
332+
if f.alternate() {
333+
write!(f, "{:?}", String::from_utf8_lossy(self.as_slice()))
334+
} else {
335+
write!(f, "{:?}", self.as_slice())
324336
}
325337
}
326338
}

0 commit comments

Comments
 (0)