Skip to content

Commit f16924a

Browse files
authored
Rollup merge of rust-lang#63421 - clarfon:escape_default, r=dtolnay
Implement Clone, Display for ascii::EscapeDefault This will mimic the same behaviour as the `char` version; `Display`ing the iterator will give its string representation without advancing it.
2 parents ab4c257 + 51ce121 commit f16924a

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/libcore/ascii.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use crate::fmt;
1515
use crate::ops::Range;
1616
use crate::iter::FusedIterator;
17+
use crate::str::from_utf8_unchecked;
1718

1819
/// An iterator over the escaped version of a byte.
1920
///
@@ -22,6 +23,7 @@ use crate::iter::FusedIterator;
2223
///
2324
/// [`escape_default`]: fn.escape_default.html
2425
#[stable(feature = "rust1", since = "1.0.0")]
26+
#[derive(Clone)]
2527
pub struct EscapeDefault {
2628
range: Range<usize>,
2729
data: [u8; 4],
@@ -130,6 +132,13 @@ impl ExactSizeIterator for EscapeDefault {}
130132
#[stable(feature = "fused", since = "1.26.0")]
131133
impl FusedIterator for EscapeDefault {}
132134

135+
#[stable(feature = "ascii_escape_display", since = "1.39.0")]
136+
impl fmt::Display for EscapeDefault {
137+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138+
f.write_str(unsafe { from_utf8_unchecked(&self.data[self.range.clone()]) })
139+
}
140+
}
141+
133142
#[stable(feature = "std_debug", since = "1.16.0")]
134143
impl fmt::Debug for EscapeDefault {
135144
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

0 commit comments

Comments
 (0)