@@ -7,7 +7,7 @@ use core::borrow::Borrow; |
|
|
7 |
7 |
use core::ffi::{CStr, c_char}; |
8 |
8 |
use core::num::NonZero; |
9 |
9 |
use core::slice::memchr; |
10 |
|
-use core::str::{self, Utf8Error}; |
|
10 |
+use core::str::{self, FromStr, Utf8Error}; |
11 |
11 |
use core::{fmt, mem, ops, ptr, slice}; |
12 |
12 |
|
13 |
13 |
use crate::borrow::{Cow, ToOwned}; |
@@ -817,6 +817,30 @@ impl From<Vec<NonZero>> for CString { |
|
|
817 |
817 |
} |
818 |
818 |
} |
819 |
819 |
|
|
820 |
+impl FromStr for CString { |
|
821 |
+type Err = NulError; |
|
822 |
+ |
|
823 |
+/// Converts a string `s` into a [`CString`]. |
|
824 |
+ /// |
|
825 |
+ /// This method is equivalent to [`CString::new`]. |
|
826 |
+ #[inline] |
|
827 |
+fn from_str(s: &str) -> Result<Self, Self::Err> { |
|
828 |
+Self::new(s) |
|
829 |
+} |
|
830 |
+} |
|
831 |
+ |
|
832 |
+impl TryFrom<CString> for String { |
|
833 |
+type Error = IntoStringError; |
|
834 |
+ |
|
835 |
+/// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data. |
|
836 |
+ /// |
|
837 |
+ /// This method is equivalent to [`CString::into_string`]. |
|
838 |
+ #[inline] |
|
839 |
+fn try_from(value: CString) -> Result<Self, Self::Error> { |
|
840 |
+ value.into_string() |
|
841 |
+} |
|
842 |
+} |
|
843 |
+ |
820 |
844 |
#[cfg(not(test))] |
821 |
845 |
#[stable(feature = "more_box_slice_clone", since = "1.29.0")] |
822 |
846 |
impl Clone for Box<CStr> { |