@@ -44,7 +44,7 @@ pub use self::methods::encode_utf8_raw; // perma-unstable
4444use crate :: ascii;
4545pub ( crate ) use self :: methods:: EscapeDebugExtArgs ;
4646use crate :: error:: Error ;
47- use crate :: escape;
47+ use crate :: escape:: { AlwaysEscaped , EscapeIterInner , MaybeEscaped } ;
4848use crate :: fmt:: { self , Write } ;
4949use crate :: iter:: { FusedIterator , TrustedLen , TrustedRandomAccess , TrustedRandomAccessNoCoerce } ;
5050use 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 , AlwaysEscaped > ) ;
165165
166166impl EscapeUnicode {
167167 #[ inline]
168168 const fn new ( c : char ) -> Self {
169- Self ( escape :: EscapeIterInner :: unicode ( c) )
169+ Self ( EscapeIterInner :: unicode ( c) )
170170 }
171171}
172172
@@ -214,8 +214,9 @@ impl FusedIterator for EscapeUnicode {}
214214
215215#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
216216impl 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
232233impl 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" ) ]
292293impl 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
318310impl 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" ) ]
368349impl 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" ) ]
381360impl 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 }
0 commit comments