File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -551,6 +551,44 @@ impl<T> IndexMut<T> for AsciiString where AsciiStr: IndexMut<T> {
551
551
}
552
552
}
553
553
554
+
555
+ /// Trait for converting vectors into `AsciiString`.
556
+ pub trait IntoAsciiString < T : ?Sized +AsciiExt < Owned =Self > > : Sized +Borrow < T > {
557
+ /// Convert to `AsciiString` without checking that every byte is less than 128.
558
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString ;
559
+ /// Convert to `AsciiString`.
560
+ fn into_ascii ( self ) -> Result < AsciiString , Self > {
561
+ if self . borrow ( ) . is_ascii ( ) {
562
+ Ok ( unsafe { self . into_ascii_unchecked ( ) } )
563
+ } else {
564
+ Err ( self )
565
+ }
566
+ }
567
+ }
568
+
569
+ #[ cfg( feature = "unstable" ) ]
570
+ impl IntoAsciiString < AsciiStr > for AsciiString {
571
+ fn into_ascii ( self ) -> Result < AsciiString , AsciiString > {
572
+ Ok ( self )
573
+ }
574
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString {
575
+ self
576
+ }
577
+ }
578
+
579
+ impl IntoAsciiString < [ u8 ] > for Vec < u8 > {
580
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString {
581
+ AsciiString :: from_bytes_unchecked ( self )
582
+ }
583
+ }
584
+
585
+ impl IntoAsciiString < str > for String {
586
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString {
587
+ self . into_bytes ( ) . into_ascii_unchecked ( )
588
+ }
589
+ }
590
+
591
+
554
592
#[ cfg( test) ]
555
593
mod tests {
556
594
use std:: str:: FromStr ;
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ use std::borrow::Borrow;
20
20
use std:: ascii:: AsciiExt ;
21
21
22
22
pub use ascii:: { Ascii , IntoAscii , IntoAsciiError } ;
23
- pub use ascii_string:: AsciiString ;
23
+ pub use ascii_string:: { AsciiString , IntoAsciiString } ;
24
24
pub use ascii_str:: AsciiStr ;
25
25
26
26
/// Trait for converting into an ascii type.
You can’t perform that action at this time.
0 commit comments