11
11
use ascii;
12
12
use borrow:: { Cow , Borrow } ;
13
13
use cmp:: Ordering ;
14
+ use convert:: TryFrom ;
14
15
use error:: Error ;
15
16
use fmt:: { self , Write } ;
16
17
use io;
@@ -646,6 +647,24 @@ impl From<CString> for Vec<u8> {
646
647
}
647
648
}
648
649
650
+ #[ unstable( feature = "try_from" , issue = "33417" ) ]
651
+ impl TryFrom < Vec < u8 > > for CString {
652
+ type Error = NulError ;
653
+
654
+ fn try_from ( bytes : Vec < u8 > ) -> Result < CString , NulError > {
655
+ CString :: _new ( bytes)
656
+ }
657
+ }
658
+
659
+ #[ unstable( feature = "try_from" , issue = "33417" ) ]
660
+ impl TryFrom < CString > for String {
661
+ type Error = IntoStringError ;
662
+
663
+ fn try_from ( c_str : CString ) -> Result < String , IntoStringError > {
664
+ c_str. into_string ( )
665
+ }
666
+ }
667
+
649
668
#[ stable( feature = "cstr_debug" , since = "1.3.0" ) ]
650
669
impl fmt:: Debug for CStr {
651
670
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -657,6 +676,15 @@ impl fmt::Debug for CStr {
657
676
}
658
677
}
659
678
679
+ #[ unstable( feature = "try_from" , issue = "33417" ) ]
680
+ impl < ' a > TryFrom < & ' a [ u8 ] > for & ' a CStr {
681
+ type Error = FromBytesWithNulError ;
682
+
683
+ fn try_from ( bytes : & [ u8 ] ) -> Result < & CStr , FromBytesWithNulError > {
684
+ CStr :: from_bytes_with_nul ( bytes)
685
+ }
686
+ }
687
+
660
688
#[ stable( feature = "cstr_default" , since = "1.10.0" ) ]
661
689
impl < ' a > Default for & ' a CStr {
662
690
fn default ( ) -> & ' a CStr {
0 commit comments