Skip to content

Commit

Permalink
Rollup merge of rust-lang#63421 - clarfon:escape_default, r=dtolnay
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Centril authored Aug 12, 2019
2 parents b15a07f + 51ce121 commit c1f65d4
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/libcore/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use crate::fmt;
use crate::ops::Range;
use crate::iter::FusedIterator;
use crate::str::from_utf8_unchecked;

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

#[stable(feature = "ascii_escape_display", since = "1.39.0")]
impl fmt::Display for EscapeDefault {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(unsafe { from_utf8_unchecked(&self.data[self.range.clone()]) })
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
impl fmt::Debug for EscapeDefault {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand Down

0 comments on commit c1f65d4

Please sign in to comment.