@@ -385,6 +385,16 @@ impl AsciiString {
385
385
pub fn clear ( & mut self ) {
386
386
self . vec . clear ( )
387
387
}
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
+ }
388
398
}
389
399
390
400
impl Deref for AsciiString {
@@ -496,6 +506,22 @@ impl Into<String> for AsciiString {
496
506
}
497
507
}
498
508
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
+
499
525
impl < ' a > From < Cow < ' a , AsciiStr > > for AsciiString {
500
526
fn from ( cow : Cow < ' a , AsciiStr > ) -> AsciiString {
501
527
cow. into_owned ( )
@@ -902,7 +928,7 @@ mod tests {
902
928
use alloc:: vec:: Vec ;
903
929
#[ cfg( feature = "std" ) ]
904
930
use std:: ffi:: CString ;
905
- use AsciiChar ;
931
+ use :: { AsciiChar , AsciiStr } ;
906
932
907
933
#[ test]
908
934
fn into_string ( ) {
@@ -971,4 +997,13 @@ mod tests {
971
997
let sparkle_heart = str:: from_utf8 ( & sparkle_heart_bytes) . unwrap ( ) ;
972
998
assert ! ( fmt:: write( & mut s2, format_args!( "{}" , sparkle_heart) ) . is_err( ) ) ;
973
999
}
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
+ }
974
1009
}
0 commit comments