File tree 2 files changed +35
-1
lines changed 2 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -551,6 +551,40 @@ impl<T> IndexMut<T> for AsciiString where AsciiStr: IndexMut<T> {
551
551
}
552
552
}
553
553
554
+ /// Trait for converting vectors into `AsciiString`.
555
+ pub trait IntoAsciiString < T : ?Sized +AsciiExt < Owned =Self > > : Sized +Borrow < T > {
556
+ /// Convert to `AsciiString` without checking that every byte is less than 128.
557
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString ;
558
+ /// Convert to `AsciiString`.
559
+ fn into_ascii ( self ) -> Result < AsciiString , Self > {
560
+ if self . borrow ( ) . is_ascii ( ) {
561
+ Ok ( unsafe { self . into_ascii_unchecked ( ) } )
562
+ } else {
563
+ Err ( self )
564
+ }
565
+ }
566
+ }
567
+
568
+ #[ cfg( feature = "unstable" ) ]
569
+ impl IntoAsciiString < AsciiStr > for AsciiString {
570
+ fn into_ascii ( self ) -> Result < AsciiString , AsciiString > {
571
+ Ok ( self )
572
+ }
573
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString {
574
+ self
575
+ }
576
+ }
577
+ impl IntoAsciiString < [ u8 ] > for Vec < u8 > {
578
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString {
579
+ AsciiString :: from_bytes_unchecked ( self )
580
+ }
581
+ }
582
+ impl IntoAsciiString < str > for String {
583
+ unsafe fn into_ascii_unchecked ( self ) -> AsciiString {
584
+ self . into_bytes ( ) . into_ascii_unchecked ( )
585
+ }
586
+ }
587
+
554
588
#[ cfg( test) ]
555
589
mod tests {
556
590
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