mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-22 03:44:24 +00:00
Implemented FromStr for CString and TryFrom<CString> for String
This commit is contained in:
parent
b0af276da3
commit
302551388b
@ -7,7 +7,7 @@ use core::borrow::Borrow;
|
|||||||
use core::ffi::{c_char, CStr};
|
use core::ffi::{c_char, CStr};
|
||||||
use core::num::NonZero;
|
use core::num::NonZero;
|
||||||
use core::slice::memchr;
|
use core::slice::memchr;
|
||||||
use core::str::{self, Utf8Error};
|
use core::str::{self, FromStr, Utf8Error};
|
||||||
use core::{fmt, mem, ops, ptr, slice};
|
use core::{fmt, mem, ops, ptr, slice};
|
||||||
|
|
||||||
use crate::borrow::{Cow, ToOwned};
|
use crate::borrow::{Cow, ToOwned};
|
||||||
@ -815,6 +815,30 @@ impl From<Vec<NonZero<u8>>> for CString {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FromStr for CString {
|
||||||
|
type Err = NulError;
|
||||||
|
|
||||||
|
/// Converts a string `s` into a [`CString`].
|
||||||
|
///
|
||||||
|
/// This method is equivalent to [`CString::new`].
|
||||||
|
#[inline]
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
Self::new(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<CString> for String {
|
||||||
|
type Error = IntoStringError;
|
||||||
|
|
||||||
|
/// Converts a [`CString`] into a [`String`] if it contains valid UTF-8 data.
|
||||||
|
///
|
||||||
|
/// This method is equivalent to [`CString::into_string`].
|
||||||
|
#[inline]
|
||||||
|
fn try_from(value: CString) -> Result<Self, Self::Error> {
|
||||||
|
value.into_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
|
#[stable(feature = "more_box_slice_clone", since = "1.29.0")]
|
||||||
impl Clone for Box<CStr> {
|
impl Clone for Box<CStr> {
|
||||||
|
Loading…
Reference in New Issue
Block a user