diff options
| author | aticu <15schnic@gmail.com> | 2022-06-17 16:31:36 +0200 |
|---|---|---|
| committer | aticu <15schnic@gmail.com> | 2023-06-12 10:46:49 +0200 |
| commit | e3a1a11ed2a1b081b229f1d873114aaf7f5f5137 (patch) | |
| tree | 5496127222a8b670726b2034e1f780cd5d66c4c0 /library/std/src/sys_common | |
| parent | 0075bb4fad68e64b6d1be06bf2db366c30bc75e1 (diff) | |
Implement `TryFrom<&OsStr>` for `&str`
Diffstat (limited to 'library/std/src/sys_common')
| -rw-r--r-- | library/std/src/sys_common/wtf8.rs | 9 | ||||
| -rw-r--r-- | library/std/src/sys_common/wtf8/tests.rs | 6 |
2 files changed, 5 insertions, 10 deletions
diff --git a/library/std/src/sys_common/wtf8.rs b/library/std/src/sys_common/wtf8.rs index 57fa4989358..f357824aee1 100644 --- a/library/std/src/sys_common/wtf8.rs +++ b/library/std/src/sys_common/wtf8.rs @@ -566,13 +566,8 @@ impl Wtf8 { /// /// This does not copy the data. #[inline] - pub fn as_str(&self) -> Option<&str> { - // Well-formed WTF-8 is also well-formed UTF-8 - // if and only if it contains no surrogate. - match self.next_surrogate(0) { - None => Some(unsafe { str::from_utf8_unchecked(&self.bytes) }), - Some(_) => None, - } + pub fn as_str(&self) -> Result<&str, str::Utf8Error> { + str::from_utf8(&self.bytes) } /// Lossily converts the string to UTF-8. diff --git a/library/std/src/sys_common/wtf8/tests.rs b/library/std/src/sys_common/wtf8/tests.rs index 931996791fb..cb6391458b0 100644 --- a/library/std/src/sys_common/wtf8/tests.rs +++ b/library/std/src/sys_common/wtf8/tests.rs @@ -354,11 +354,11 @@ fn wtf8_code_points() { #[test] fn wtf8_as_str() { - assert_eq!(Wtf8::from_str("").as_str(), Some("")); - assert_eq!(Wtf8::from_str("aé 💩").as_str(), Some("aé 💩")); + assert_eq!(Wtf8::from_str("").as_str(), Ok("")); + assert_eq!(Wtf8::from_str("aé 💩").as_str(), Ok("aé 💩")); let mut string = Wtf8Buf::new(); string.push(CodePoint::from_u32(0xD800).unwrap()); - assert_eq!(string.as_str(), None); + assert!(string.as_str().is_err()); } #[test] |
