Skip to content

Commit 3025513

Browse files
committed
Implemented FromStr for CString and TryFrom<CString> for String
1 parent b0af276 commit 3025513

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

‎library/alloc/src/ffi/c_str.rs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::borrow::Borrow;
77
use core::ffi::{c_char,CStr};
88
use core::num::NonZero;
99
use core::slice::memchr;
10-
use core::str::{self,Utf8Error};
10+
use core::str::{self,FromStr,Utf8Error};
1111
use core::{fmt, mem, ops, ptr, slice};
1212

1313
usecrate::borrow::{Cow,ToOwned};
@@ -815,6 +815,30 @@ impl From<Vec<NonZero<u8>>> for CString {
815815
}
816816
}
817817

818+
implFromStrforCString{
819+
typeErr = NulError;
820+
821+
/// Converts a string `s` into a [`CString`].
822+
///
823+
/// This method is equivalent to [`CString::new`].
824+
#[inline]
825+
fnfrom_str(s:&str) -> Result<Self,Self::Err>{
826+
Self::new(s)
827+
}
828+
}
829+
830+
implTryFrom<CString>forString{
831+
typeError = IntoStringError;
832+
833+
/// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data.
834+
///
835+
/// This method is equivalent to [`CString::into_string`].
836+
#[inline]
837+
fntry_from(value:CString) -> Result<Self,Self::Error>{
838+
value.into_string()
839+
}
840+
}
841+
818842
#[cfg(not(test))]
819843
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
820844
implCloneforBox<CStr>{

0 commit comments

Comments
 (0)