Skip to content

Get rid of EscapeDebugInner. #138237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions library/core/src/ascii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

#![stable(feature = "core_ascii", since = "1.26.0")]

use crate::escape::{AlwaysEscaped, EscapeIterInner};
use crate::fmt;
use crate::iter::FusedIterator;
use crate::num::NonZero;
use crate::{escape, fmt};

mod ascii_char;
#[unstable(feature = "ascii_char", issue = "110998")]
Expand All @@ -24,7 +25,7 @@ pub use ascii_char::AsciiChar as Char;
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Clone)]
pub struct EscapeDefault(escape::EscapeIterInner<4>);
pub struct EscapeDefault(EscapeIterInner<4, AlwaysEscaped>);

/// Returns an iterator that produces an escaped version of a `u8`.
///
Expand Down Expand Up @@ -96,17 +97,12 @@ pub fn escape_default(c: u8) -> EscapeDefault {
impl EscapeDefault {
#[inline]
pub(crate) const fn new(c: u8) -> Self {
Self(escape::EscapeIterInner::ascii(c))
Self(EscapeIterInner::ascii(c))
}

#[inline]
pub(crate) fn empty() -> Self {
Self(escape::EscapeIterInner::empty())
}

#[inline]
pub(crate) fn as_str(&self) -> &str {
self.0.as_str()
Self(EscapeIterInner::empty())
}
}

Expand Down Expand Up @@ -168,7 +164,7 @@ 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(self.0.as_str())
fmt::Display::fmt(&self.0, f)
}
}

Expand Down
64 changes: 21 additions & 43 deletions library/core/src/char/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use self::methods::encode_utf8_raw; // perma-unstable
use crate::ascii;
pub(crate) use self::methods::EscapeDebugExtArgs;
use crate::error::Error;
use crate::escape;
use crate::escape::{AlwaysEscaped, EscapeIterInner, MaybeEscaped};
use crate::fmt::{self, Write};
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
use crate::num::NonZero;
Expand Down Expand Up @@ -161,12 +161,12 @@ pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
/// [`escape_unicode`]: char::escape_unicode
#[derive(Clone, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct EscapeUnicode(escape::EscapeIterInner<10>);
pub struct EscapeUnicode(EscapeIterInner<10, AlwaysEscaped>);

impl EscapeUnicode {
#[inline]
const fn new(c: char) -> Self {
Self(escape::EscapeIterInner::unicode(c))
Self(EscapeIterInner::unicode(c))
}
}

Expand Down Expand Up @@ -214,8 +214,9 @@ impl FusedIterator for EscapeUnicode {}

#[stable(feature = "char_struct_display", since = "1.16.0")]
impl fmt::Display for EscapeUnicode {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.0.as_str())
fmt::Display::fmt(&self.0, f)
}
}

Expand All @@ -227,22 +228,22 @@ impl fmt::Display for EscapeUnicode {
/// [`escape_default`]: char::escape_default
#[derive(Clone, Debug)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct EscapeDefault(escape::EscapeIterInner<10>);
pub struct EscapeDefault(EscapeIterInner<10, AlwaysEscaped>);

impl EscapeDefault {
#[inline]
const fn printable(c: ascii::Char) -> Self {
Self(escape::EscapeIterInner::ascii(c.to_u8()))
Self(EscapeIterInner::ascii(c.to_u8()))
}

#[inline]
const fn backslash(c: ascii::Char) -> Self {
Self(escape::EscapeIterInner::backslash(c))
Self(EscapeIterInner::backslash(c))
}

#[inline]
const fn unicode(c: char) -> Self {
Self(escape::EscapeIterInner::unicode(c))
Self(EscapeIterInner::unicode(c))
}
}

Expand Down Expand Up @@ -290,8 +291,9 @@ impl FusedIterator for EscapeDefault {}

#[stable(feature = "char_struct_display", since = "1.16.0")]
impl fmt::Display for EscapeDefault {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.0.as_str())
fmt::Display::fmt(&self.0, f)
}
}

Expand All @@ -303,37 +305,22 @@ impl fmt::Display for EscapeDefault {
/// [`escape_debug`]: char::escape_debug
#[stable(feature = "char_escape_debug", since = "1.20.0")]
#[derive(Clone, Debug)]
pub struct EscapeDebug(EscapeDebugInner);

#[derive(Clone, Debug)]
// Note: It’s possible to manually encode the EscapeDebugInner inside of
// EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4] holds
// a char) which would likely result in a more optimised code. For now we use
// the option easier to implement.
Comment on lines -309 to -312
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the comment you reference, so perfrun seems to be in order.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Should definitely be optimized in terms of size, speed remains to be seen.

enum EscapeDebugInner {
Bytes(escape::EscapeIterInner<10>),
Char(char),
}
pub struct EscapeDebug(EscapeIterInner<10, MaybeEscaped>);

impl EscapeDebug {
#[inline]
const fn printable(chr: char) -> Self {
Self(EscapeDebugInner::Char(chr))
Self(EscapeIterInner::printable(chr))
}

#[inline]
const fn backslash(c: ascii::Char) -> Self {
Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::backslash(c)))
Self(EscapeIterInner::backslash(c))
}

#[inline]
const fn unicode(c: char) -> Self {
Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::unicode(c)))
}

#[inline]
fn clear(&mut self) {
self.0 = EscapeDebugInner::Bytes(escape::EscapeIterInner::empty());
Self(EscapeIterInner::unicode(c))
}
}

Expand All @@ -343,13 +330,7 @@ impl Iterator for EscapeDebug {

#[inline]
fn next(&mut self) -> Option<char> {
match self.0 {
EscapeDebugInner::Bytes(ref mut bytes) => bytes.next().map(char::from),
EscapeDebugInner::Char(chr) => {
self.clear();
Some(chr)
}
}
self.0.next()
}

#[inline]
Expand All @@ -366,11 +347,9 @@ impl Iterator for EscapeDebug {

#[stable(feature = "char_escape_debug", since = "1.20.0")]
impl ExactSizeIterator for EscapeDebug {
#[inline]
fn len(&self) -> usize {
match &self.0 {
EscapeDebugInner::Bytes(bytes) => bytes.len(),
EscapeDebugInner::Char(_) => 1,
}
self.0.len()
}
}

Expand All @@ -379,11 +358,9 @@ impl FusedIterator for EscapeDebug {}

#[stable(feature = "char_escape_debug", since = "1.20.0")]
impl fmt::Display for EscapeDebug {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
EscapeDebugInner::Bytes(bytes) => f.write_str(bytes.as_str()),
EscapeDebugInner::Char(chr) => f.write_char(*chr),
}
fmt::Display::fmt(&self.0, f)
}
}

Expand Down Expand Up @@ -480,6 +457,7 @@ macro_rules! casemappingiter_impls {

#[stable(feature = "char_struct_display", since = "1.16.0")]
impl fmt::Display for $ITER_NAME {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
Expand Down
Loading
Loading