@@ -385,6 +385,16 @@ impl AsciiString {
385385 pub fn clear ( & mut self ) {
386386 self . vec . clear ( )
387387 }
388+
389+ /// Converts this [`AsciiString`] into a [`Box`]`<`[`AsciiStr`]`>`.
390+ ///
391+ /// This will drop any excess capacity
392+ #[ cfg( feature = "alloc" ) ]
393+ #[ inline]
394+ pub fn into_boxed_ascii_str ( self ) -> Box < AsciiStr > {
395+ let slice = self . vec . into_boxed_slice ( ) ;
396+ Box :: from ( slice)
397+ }
388398}
389399
390400impl Deref for AsciiString {
@@ -496,6 +506,22 @@ impl Into<String> for AsciiString {
496506 }
497507}
498508
509+ #[ cfg( feature = "alloc" ) ]
510+ impl From < Box < AsciiStr > > for AsciiString {
511+ #[ inline]
512+ fn from ( boxed : Box < AsciiStr > ) -> Self {
513+ boxed. into_ascii_string ( )
514+ }
515+ }
516+
517+ #[ cfg( feature = "alloc" ) ]
518+ impl From < AsciiString > for Box < AsciiStr > {
519+ #[ inline]
520+ fn from ( string : AsciiString ) -> Self {
521+ string. into_boxed_ascii_str ( )
522+ }
523+ }
524+
499525impl < ' a > From < Cow < ' a , AsciiStr > > for AsciiString {
500526 fn from ( cow : Cow < ' a , AsciiStr > ) -> AsciiString {
501527 cow. into_owned ( )
@@ -902,7 +928,7 @@ mod tests {
902928 use alloc:: vec:: Vec ;
903929 #[ cfg( feature = "std" ) ]
904930 use std:: ffi:: CString ;
905- use AsciiChar ;
931+ use :: { AsciiChar , AsciiStr } ;
906932
907933 #[ test]
908934 fn into_string ( ) {
@@ -971,4 +997,13 @@ mod tests {
971997 let sparkle_heart = str:: from_utf8 ( & sparkle_heart_bytes) . unwrap ( ) ;
972998 assert ! ( fmt:: write( & mut s2, format_args!( "{}" , sparkle_heart) ) . is_err( ) ) ;
973999 }
1000+
1001+ #[ cfg( feature = "alloc" ) ]
1002+ #[ test]
1003+ fn to_and_from_box ( ) {
1004+ let string = "abc" . into_ascii_string ( ) . unwrap ( ) ;
1005+ let converted: Box < AsciiStr > = Box :: from ( string. clone ( ) ) ;
1006+ let converted: AsciiString = converted. into ( ) ;
1007+ assert_eq ! ( string, converted) ;
1008+ }
9741009}
0 commit comments