Skip to content

Add OsStr::from_bytes() #26730

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

Closed
wants to merge 2 commits into from
Closed

Add OsStr::from_bytes() #26730

wants to merge 2 commits into from

Conversation

remram44
Copy link
Contributor

@remram44 remram44 commented Jul 2, 2015

OsString::from_bytes() turns Vec<u8> into OsString. This adds the same thing, from &[u8] to &OsStr:

pub fn OsString::from_bytes(vec: Vec<u8>) -> Option<OsString>
pub fn OsStr::from_bytes(bytes: &[u8]) -> Option<&OsStr>

Problem is that OsStrExt has a conflicting from_bytes() (that always succeeds; unix-only). This PR removes it, which might need an RFC (it was Rust 1.0.0).
OsStrExt then only has an always-succeeding as_bytes(), which could also be replaced by to_bytes().unwrap() without loss of functionality.

@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 @nikomatsakis (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.

@remram44 remram44 mentioned this pull request Jul 2, 2015
@@ -43,9 +42,6 @@ impl OsStringExt for OsString {
/// Unix-specific extensions to `OsStr`.
#[stable(feature = "rust1", since = "1.0.0")]
pub trait OsStrExt {
#[stable(feature = "rust1", since = "1.0.0")]
fn from_bytes(slice: &[u8]) -> &Self;
Copy link
Member

Choose a reason for hiding this comment

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

This is currently a stable method, so it can't be removed.

@alexcrichton
Copy link
Member

I like the idea of this, but I'm worried about this breaking code in practice. OsStrExt::from_bytes is commonly called as OsStr::from_bytes, so this will break all callers of the method today. We may want to consider a different name for this method.

@remram44
Copy link
Contributor Author

remram44 commented Jul 2, 2015

Sure, that's certainly much more compatible. Though we have to choose between symmetry with OsString (which has from_bytes()) and compatibility (keeping the current OsStrExt). I can do the rename if you want.

@alexcrichton
Copy link
Member

We cannot compromise on compatibility, so while it's unfortunate that the symmetry is lost here we don't have any other recourse.

@remram44
Copy link
Contributor Author

remram44 commented Jul 2, 2015

Understood, that is very clear.
Any idea for the new name? from_slice? from_u8? from_u8_slice? from_bytes_checked?

@alexcrichton
Copy link
Member

Perhaps from_bytes_opt but I don't think we use the _opt suffix that often any more.

@remram44
Copy link
Contributor Author

remram44 commented Jul 5, 2015

It seems to me that the best approaches are

  • to give the OsStr method a name that makes it clear why it's different from the OsString one (something like from_bytes_slice; this is the new patch), or
  • to change the name of both of them (make this from_bytes_opt, and rename OsString's from_bytes to from_bytes_opt as well; adding this alias can be done in a 100% compatible way).

Should be from_bytes(), since that the name of the method in OsString;
however this is already used in OsStrExt on Unix. Thus
from_bytes_slice(), which makes it clear why the name is different (this
one operates on slices without copying).
@alexcrichton
Copy link
Member

I think that the current strategy taken is probably the best course of action, but I'd recommend the name from_bytes_opt for now. The OsString::from_bytes method is currently still unstable so we can rename it as appropriate as well.

This makes it possible to rename OsString::from_bytes() as well, since
it is currently unstable (feature(convert)).
@remram44
Copy link
Contributor Author

remram44 commented Jul 6, 2015

Sure thing!

@alexcrichton alexcrichton added I-needs-decision Issue: In need of a decision. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jul 6, 2015
@alexcrichton
Copy link
Member

Looks good to me, thanks @remram44! Tagging with libs/needs-decision so this comes up in triage.

@alexcrichton
Copy link
Member

Ok we talked about this in the triage meeting for libs this week, and we reached two conclusions:

  • The suffix _opt should be avoided as we've done it before and it never panned out too well
  • The OsString::from_bytes method should be renamed to whatever this method is.

Unfortunately there weren't any new insights about what the method should be called, but it was suggested that it should emphasize perhaps the parsing aspect for some platforms (e.g. str::from_utf8 on Windows).

@alexcrichton alexcrichton removed the I-needs-decision Issue: In need of a decision. label Jul 8, 2015
@aturon
Copy link
Member

aturon commented Jul 8, 2015

from_platform_bytes perhaps?

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with the a renaming!

@remram44
Copy link
Contributor Author

I did the rename, you can reopen. Not sure if the old OsString::from_bytes() should be deprecated or just go away (it was never stable). Currently trying to run tests.

@alexcrichton
Copy link
Member

Oh weird it looks like github doesn't allow me to reopen after a force push of the branch... Could you make a new PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants