File tree 5 files changed +27
-14
lines changed 5 files changed +27
-14
lines changed Original file line number Diff line number Diff line change @@ -692,7 +692,7 @@ impl OsStr {
692
692
without modifying the original"]
693
693
#[ inline]
694
694
pub fn to_str ( & self ) -> Option < & str > {
695
- self . inner . to_str ( )
695
+ self . inner . to_str ( ) . ok ( )
696
696
}
697
697
698
698
/// Converts an `OsStr` to a <code>[Cow]<[str]></code>.
@@ -1101,6 +1101,24 @@ impl<'a> From<Cow<'a, OsStr>> for OsString {
1101
1101
}
1102
1102
}
1103
1103
1104
+ #[ stable( feature = "str_tryfrom_osstr_impl" , since = "CURRENT_RUSTC_VERSION" ) ]
1105
+ impl < ' a > TryFrom < & ' a OsStr > for & ' a str {
1106
+ type Error = crate :: str:: Utf8Error ;
1107
+
1108
+ /// Tries to convert an `&OsStr` to a `&str`.
1109
+ ///
1110
+ /// ```
1111
+ /// use std::ffi::OsStr;
1112
+ ///
1113
+ /// let os_str = OsStr::new("foo");
1114
+ /// let as_str = <&str>::try_from(os_str).unwrap();
1115
+ /// assert_eq!(as_str, "foo");
1116
+ /// ```
1117
+ fn try_from ( value : & ' a OsStr ) -> Result < Self , Self :: Error > {
1118
+ value. inner . to_str ( )
1119
+ }
1120
+ }
1121
+
1104
1122
#[ stable( feature = "box_default_extra" , since = "1.17.0" ) ]
1105
1123
impl Default for Box < OsStr > {
1106
1124
#[ inline]
Original file line number Diff line number Diff line change @@ -195,8 +195,8 @@ impl Slice {
195
195
Slice :: from_u8_slice ( s. as_bytes ( ) )
196
196
}
197
197
198
- pub fn to_str ( & self ) -> Option < & str > {
199
- str:: from_utf8 ( & self . inner ) . ok ( )
198
+ pub fn to_str ( & self ) -> Result < & str , crate :: str :: Utf8Error > {
199
+ str:: from_utf8 ( & self . inner )
200
200
}
201
201
202
202
pub fn to_string_lossy ( & self ) -> Cow < ' _ , str > {
Original file line number Diff line number Diff line change @@ -155,7 +155,7 @@ impl Slice {
155
155
unsafe { mem:: transmute ( Wtf8 :: from_str ( s) ) }
156
156
}
157
157
158
- pub fn to_str ( & self ) -> Option < & str > {
158
+ pub fn to_str ( & self ) -> Result < & str , crate :: str :: Utf8Error > {
159
159
self . inner . as_str ( )
160
160
}
161
161
Original file line number Diff line number Diff line change @@ -566,13 +566,8 @@ impl Wtf8 {
566
566
///
567
567
/// This does not copy the data.
568
568
#[ inline]
569
- pub fn as_str ( & self ) -> Option < & str > {
570
- // Well-formed WTF-8 is also well-formed UTF-8
571
- // if and only if it contains no surrogate.
572
- match self . next_surrogate ( 0 ) {
573
- None => Some ( unsafe { str:: from_utf8_unchecked ( & self . bytes ) } ) ,
574
- Some ( _) => None ,
575
- }
569
+ pub fn as_str ( & self ) -> Result < & str , str:: Utf8Error > {
570
+ str:: from_utf8 ( & self . bytes )
576
571
}
577
572
578
573
/// Lossily converts the string to UTF-8.
Original file line number Diff line number Diff line change @@ -354,11 +354,11 @@ fn wtf8_code_points() {
354
354
355
355
#[ test]
356
356
fn wtf8_as_str ( ) {
357
- assert_eq ! ( Wtf8 :: from_str( "" ) . as_str( ) , Some ( "" ) ) ;
358
- assert_eq ! ( Wtf8 :: from_str( "aé 💩" ) . as_str( ) , Some ( "aé 💩" ) ) ;
357
+ assert_eq ! ( Wtf8 :: from_str( "" ) . as_str( ) , Ok ( "" ) ) ;
358
+ assert_eq ! ( Wtf8 :: from_str( "aé 💩" ) . as_str( ) , Ok ( "aé 💩" ) ) ;
359
359
let mut string = Wtf8Buf :: new ( ) ;
360
360
string. push ( CodePoint :: from_u32 ( 0xD800 ) . unwrap ( ) ) ;
361
- assert_eq ! ( string. as_str( ) , None ) ;
361
+ assert ! ( string. as_str( ) . is_err ( ) ) ;
362
362
}
363
363
364
364
#[ test]
You can’t perform that action at this time.
0 commit comments