Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1c078d

Browse files
committedMar 8, 2025
Get rid of EscapeDebugInner.
1 parent 07292cc commit e1c078d

File tree

4 files changed

+188
-80
lines changed

4 files changed

+188
-80
lines changed
 

‎library/core/src/ascii.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
1010
#![stable(feature = "core_ascii", since = "1.26.0")]
1111

12+
use crate::escape::{AlwaysEscaped, EscapeIterInner};
13+
use crate::fmt;
1214
use crate::iter::FusedIterator;
1315
use crate::num::NonZero;
14-
use crate::{escape, fmt};
1516

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

2930
/// Returns an iterator that produces an escaped version of a `u8`.
3031
///
@@ -96,17 +97,12 @@ pub fn escape_default(c: u8) -> EscapeDefault {
9697
impl EscapeDefault {
9798
#[inline]
9899
pub(crate) const fn new(c: u8) -> Self {
99-
Self(escape::EscapeIterInner::ascii(c))
100+
Self(EscapeIterInner::ascii(c))
100101
}
101102

102103
#[inline]
103104
pub(crate) fn empty() -> Self {
104-
Self(escape::EscapeIterInner::empty())
105-
}
106-
107-
#[inline]
108-
pub(crate) fn as_str(&self) -> &str {
109-
self.0.as_str()
105+
Self(EscapeIterInner::empty())
110106
}
111107
}
112108

@@ -168,7 +164,7 @@ impl FusedIterator for EscapeDefault {}
168164
#[stable(feature = "ascii_escape_display", since = "1.39.0")]
169165
impl fmt::Display for EscapeDefault {
170166
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171-
f.write_str(self.0.as_str())
167+
fmt::Display::fmt(&self.0, f)
172168
}
173169
}
174170

‎library/core/src/char/mod.rs

+22-44
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub use self::methods::encode_utf8_raw; // perma-unstable
4444
use crate::ascii;
4545
pub(crate) use self::methods::EscapeDebugExtArgs;
4646
use crate::error::Error;
47-
use crate::escape;
47+
use crate::escape::{AlwaysEscaped, EscapeIterInner, MaybeEscaped};
4848
use crate::fmt::{self, Write};
4949
use crate::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
5050
use crate::num::NonZero;
@@ -161,12 +161,12 @@ pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
161161
/// [`escape_unicode`]: char::escape_unicode
162162
#[derive(Clone, Debug)]
163163
#[stable(feature = "rust1", since = "1.0.0")]
164-
pub struct EscapeUnicode(escape::EscapeIterInner<10>);
164+
pub struct EscapeUnicode(EscapeIterInner<10, MaybeEscaped>);
165165

166166
impl EscapeUnicode {
167167
#[inline]
168168
const fn new(c: char) -> Self {
169-
Self(escape::EscapeIterInner::unicode(c))
169+
Self(EscapeIterInner::unicode(c))
170170
}
171171
}
172172

@@ -192,7 +192,7 @@ impl Iterator for EscapeUnicode {
192192

193193
#[inline]
194194
fn last(mut self) -> Option<char> {
195-
self.0.next_back().map(char::from)
195+
self.0.next_back()
196196
}
197197

198198
#[inline]
@@ -214,8 +214,9 @@ impl FusedIterator for EscapeUnicode {}
214214

215215
#[stable(feature = "char_struct_display", since = "1.16.0")]
216216
impl fmt::Display for EscapeUnicode {
217+
#[inline]
217218
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218-
f.write_str(self.0.as_str())
219+
fmt::Display::fmt(&self.0, f)
219220
}
220221
}
221222

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

232233
impl EscapeDefault {
233234
#[inline]
234235
const fn printable(c: ascii::Char) -> Self {
235-
Self(escape::EscapeIterInner::ascii(c.to_u8()))
236+
Self(EscapeIterInner::ascii(c.to_u8()))
236237
}
237238

238239
#[inline]
239240
const fn backslash(c: ascii::Char) -> Self {
240-
Self(escape::EscapeIterInner::backslash(c))
241+
Self(EscapeIterInner::backslash(c))
241242
}
242243

243244
#[inline]
244245
const fn unicode(c: char) -> Self {
245-
Self(escape::EscapeIterInner::unicode(c))
246+
Self(EscapeIterInner::unicode(c))
246247
}
247248
}
248249

@@ -290,8 +291,9 @@ impl FusedIterator for EscapeDefault {}
290291

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

@@ -303,37 +305,22 @@ impl fmt::Display for EscapeDefault {
303305
/// [`escape_debug`]: char::escape_debug
304306
#[stable(feature = "char_escape_debug", since = "1.20.0")]
305307
#[derive(Clone, Debug)]
306-
pub struct EscapeDebug(EscapeDebugInner);
307-
308-
#[derive(Clone, Debug)]
309-
// Note: It’s possible to manually encode the EscapeDebugInner inside of
310-
// EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4] holds
311-
// a char) which would likely result in a more optimised code. For now we use
312-
// the option easier to implement.
313-
enum EscapeDebugInner {
314-
Bytes(escape::EscapeIterInner<10>),
315-
Char(char),
316-
}
308+
pub struct EscapeDebug(EscapeIterInner<10, MaybeEscaped>);
317309

318310
impl EscapeDebug {
319311
#[inline]
320312
const fn printable(chr: char) -> Self {
321-
Self(EscapeDebugInner::Char(chr))
313+
Self(EscapeIterInner::printable(chr))
322314
}
323315

324316
#[inline]
325317
const fn backslash(c: ascii::Char) -> Self {
326-
Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::backslash(c)))
318+
Self(EscapeIterInner::backslash(c))
327319
}
328320

329321
#[inline]
330322
const fn unicode(c: char) -> Self {
331-
Self(EscapeDebugInner::Bytes(escape::EscapeIterInner::unicode(c)))
332-
}
333-
334-
#[inline]
335-
fn clear(&mut self) {
336-
self.0 = EscapeDebugInner::Bytes(escape::EscapeIterInner::empty());
323+
Self(EscapeIterInner::unicode(c))
337324
}
338325
}
339326

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

344331
#[inline]
345332
fn next(&mut self) -> Option<char> {
346-
match self.0 {
347-
EscapeDebugInner::Bytes(ref mut bytes) => bytes.next().map(char::from),
348-
EscapeDebugInner::Char(chr) => {
349-
self.clear();
350-
Some(chr)
351-
}
352-
}
333+
self.0.next()
353334
}
354335

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

367348
#[stable(feature = "char_escape_debug", since = "1.20.0")]
368349
impl ExactSizeIterator for EscapeDebug {
350+
#[inline]
369351
fn len(&self) -> usize {
370-
match &self.0 {
371-
EscapeDebugInner::Bytes(bytes) => bytes.len(),
372-
EscapeDebugInner::Char(_) => 1,
373-
}
352+
self.0.len()
374353
}
375354
}
376355

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

380359
#[stable(feature = "char_escape_debug", since = "1.20.0")]
381360
impl fmt::Display for EscapeDebug {
361+
#[inline]
382362
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
383-
match &self.0 {
384-
EscapeDebugInner::Bytes(bytes) => f.write_str(bytes.as_str()),
385-
EscapeDebugInner::Char(chr) => f.write_char(*chr),
386-
}
363+
fmt::Display::fmt(&self.0, f)
387364
}
388365
}
389366

@@ -480,6 +457,7 @@ macro_rules! casemappingiter_impls {
480457

481458
#[stable(feature = "char_struct_display", since = "1.16.0")]
482459
impl fmt::Display for $ITER_NAME {
460+
#[inline]
483461
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
484462
fmt::Display::fmt(&self.0, f)
485463
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.