+ /// Converts the `CString` into a `String` if it contains valid Unicode data.
+ ///
+ /// On failure, ownership of the original `CString` is returned.
+ #[unstable(feature = "cstring_into", reason = "recently added", issue = "29157")]
+ pub fn into_string(self) -> Result<String, IntoStringError> {
+ String::from_utf8(self.into_bytes())
+ .map_err(|e| IntoStringError {
+ error: e.utf8_error(),
+ inner: unsafe { CString::from_vec_unchecked(e.into_bytes()) },
+ })
+ }
+
+ /// Returns the underlying byte buffer.
+ ///
+ /// The returned buffer does **not** contain the trailing nul separator and
+ /// it is guaranteed to not have any interior nul bytes.
+ #[unstable(feature = "cstring_into", reason = "recently added", issue = "29157")]
+ pub fn into_bytes(self) -> Vec<u8> {
+ // FIXME: Once this method becomes stable, add an `impl Into<Vec<u8>> for CString`
+ let mut vec = self.inner.into_vec();
+ let _nul = vec.pop();
+ debug_assert_eq!(_nul, Some(0u8));
+ vec
+ }
+
+ /// Equivalent to the `into_bytes` function except that the returned vector
+ /// includes the trailing nul byte.
+ #[unstable(feature = "cstring_into", reason = "recently added", issue = "29157")]
+ pub fn into_bytes_with_nul(self) -> Vec<u8> {
+ self.inner.into_vec()
+ }
+