]> git.lizzy.rs Git - rust.git/commitdiff
Add a TryFrom<Vec<u8>> impl that mirror from_vec_with_nul
authorAlexis Bourget <alexis.bourget@gmail.com>
Wed, 10 Jun 2020 21:13:45 +0000 (23:13 +0200)
committerAlexis Bourget <alexis.bourget@gmail.com>
Wed, 10 Jun 2020 22:36:55 +0000 (00:36 +0200)
src/libstd/ffi/c_str.rs

index 6f7dc091897f4d460676eaa35af695e1c330176f..3a3b51fd353b2ad64d0b5ce9718f2303037ebb54 100644 (file)
@@ -1,6 +1,7 @@
 use crate::ascii;
 use crate::borrow::{Borrow, Cow};
 use crate::cmp::Ordering;
+use crate::convert::TryFrom;
 use crate::error::Error;
 use crate::fmt::{self, Write};
 use crate::io;
@@ -853,6 +854,19 @@ fn from(v: Vec<NonZeroU8>) -> CString {
     }
 }
 
+#[unstable(feature = "cstring_from_vec_with_nul", issue = "73179")]
+impl TryFrom<Vec<u8>> for CString {
+    type Error = FromBytesWithNulError;
+
+    /// See the document about [`from_vec_with_nul`] for more
+    /// informations about the behaviour of this method.
+    ///
+    /// [`from_vec_with_nul`]: struct.CString.html#method.from_vec_with_nul
+    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
+        Self::from_vec_with_nul(value)
+    }
+}
+
 #[stable(feature = "more_box_slice_clone", since = "1.29.0")]
 impl Clone for Box<CStr> {
     #[inline]