Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TryFrom for CString and CStr #44916

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use ascii;
use borrow::{Cow, Borrow};
use cmp::Ordering;
use convert::TryFrom;
use error::Error;
use fmt::{self, Write};
use io;
Expand Down Expand Up @@ -646,6 +647,24 @@ impl From<CString> for Vec<u8> {
}
}

#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<Vec<u8>> for CString {
type Error = NulError;

fn try_from(bytes: Vec<u8>) -> Result<CString, NulError> {
CString::_new(bytes)
}
}

#[unstable(feature = "try_from", issue = "33417")]
impl TryFrom<CString> for String {
type Error = IntoStringError;

fn try_from(c_str: CString) -> Result<String, IntoStringError> {
c_str.into_string()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This checks for valid UTF-8 so this impl should also be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryFrom<CString> for String is an obvious conversion. The underlying conversion checks valid UTF-8 but CString is a named and obvious type unlike Vec<u8>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A CString is a sequence of none nul bytes. A Vec<u8> is a sequence of bytes. The conversion to String is exactly the same so it doesn't make sense to include one but not the other.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennytm @sfackler do you guys have any input on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd lean towards leaving this out for now.

}
}

#[stable(feature = "cstr_debug", since = "1.3.0")]
impl fmt::Debug for CStr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -657,6 +676,15 @@ impl fmt::Debug for CStr {
}
}

#[unstable(feature = "try_from", issue = "33417")]
impl<'a> TryFrom<&'a [u8]> for &'a CStr {
type Error = FromBytesWithNulError;

fn try_from(bytes: &[u8]) -> Result<&CStr, FromBytesWithNulError> {
CStr::from_bytes_with_nul(bytes)
}
}

#[stable(feature = "cstr_default", since = "1.10.0")]
impl<'a> Default for &'a CStr {
fn default() -> &'a CStr {
Expand Down