Skip to content

Owned conversions for CString #28977

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

Merged
merged 1 commit into from
Oct 19, 2015
Merged

Owned conversions for CString #28977

merged 1 commit into from
Oct 19, 2015

Conversation

arcnmx
Copy link
Contributor

@arcnmx arcnmx commented Oct 11, 2015

OsString has these sorts of conversions, while CString has been missing them. I'm iffy on into_string simply because the return type would be better off as FromUtf8Error<CString>, which of course isn't generic 😢

Also should a different/new feature gate be used?

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pcwalton (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

///
/// On failure, ownership of the original `CString` is returned.
#[unstable(feature = "convert", reason = "recently added", issue = "27704")]
pub fn into_string(self) -> Result<String, CString> {
Copy link
Member

Choose a reason for hiding this comment

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

Unlike OsString's into_string method this actually has some extra contextual information to give back (e.g. the str::Utf8Error). Along those lines this may want to return its own custom error type (e.g. the same as FromUtf8Error but with a CString instead of String.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you suggest what this type should be called? Would the best course of action be to just create std::ffi::FromUtf8Error or name it CStringFromUtf8Error or..?

Copy link
Member

Choose a reason for hiding this comment

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

This would probably be called IntoStringError

@alexcrichton
Copy link
Member

Thanks @arcnmx! These look pretty good to me, but gonna tag with T-libs to discuss a bit. I think we'll also want to connect these to a new tracking issue before landing (but that can hold off for a bit)

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 12, 2015
@brson brson added the relnotes Marks issues that should be documented in the release notes of the next release. label Oct 14, 2015
@alexcrichton
Copy link
Member

We chatted about this in the libs team meeting today and were fine moving forward with it. We also thought that it would be a good idea to hold off on an Into implementation as it's insta-stable, but could a comment be left next to into_bytes indicating that when it's stabilized a trait implementation of Into should also be provided?

@arcnmx
Copy link
Contributor Author

arcnmx commented Oct 17, 2015

Included the requested comment and created an IntoStringError type. Also cleared the issue number to 0, not quite sure what to do with that.

/// Consumes this error, returning original `CString` which generated the
/// error.
#[unstable(feature = "convert", reason = "recently added", issue = "0")]
pub fn into_inner(self) -> CString {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this instead be called into_cstring? There's some precedent here (NulError::into_vec and FromUtf8Error::into_bytes) but they're not even consistent so I wasn't sure...

Copy link
Member

Choose a reason for hiding this comment

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

Yeah let's call this into_cstring for now to stick with into_<type> of the other errors.

/// An error returned from `CString::into_string` to indicate that a UTF-8 error
/// was encountered during the conversion.
#[derive(Clone, PartialEq, Debug)]
#[unstable(feature = "convert", reason = "recently added", issue = "0")]
Copy link
Member

Choose a reason for hiding this comment

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

I've created #29157 to connect all these features, to, so could you connect them via issue to that and then also update the feature name here to something like cstring_into or something similar?

@arcnmx
Copy link
Contributor Author

arcnmx commented Oct 19, 2015

Changes made!

@alexcrichton
Copy link
Member

Looks good to me! Could you also squash down to one commit? Other than that r=me

@arcnmx
Copy link
Contributor Author

arcnmx commented Oct 19, 2015

Squashed!

@alexcrichton
Copy link
Member

@bors: r+ cb48d44

Thanks!

bors added a commit that referenced this pull request Oct 19, 2015
`OsString` has these sorts of conversions, while `CString` has been missing them. I'm iffy on `into_string` simply because the return type would be better off as `FromUtf8Error<CString>`, which of course isn't generic 😢

Also should a different/new feature gate be used?
@bors
Copy link
Collaborator

bors commented Oct 19, 2015

⌛ Testing commit cb48d44 with merge c74454a...

@bors bors merged commit cb48d44 into rust-lang:master Oct 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants