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

Conversions between CStr, OsStr, Path and boxes #39594

Merged
merged 1 commit into from
Feb 15, 2017

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Feb 6, 2017

This closes a bit of the inconsistencies between CStr, OsStr, Path, and str, allowing people to create boxed versions of DSTs other than str and [T].

Full list of additions:

  • Default for Box<str>, Box<CStr>, Box<OsStr>
  • CString::into_boxed_c_str (feature gated)
  • OsString::into_boxed_os_str (feature gated)
  • Path::into_boxed_path (feature gated)
  • From<&CStr> for Box<CStr>
  • From<&OsStr> for Box<OsStr>
  • From<&Path> for Box<Path>

This also includes adding the internal methods:

  • sys::*::os_str::Buf::into_box
  • sys::*::os_str::Slice::{into_box, empty_box}
  • sys_common::wtf8::Wtf8Buf::into_box
  • sys_common::wtf8::Wtf8::{into_box, empty_box}

@rust-highfive
Copy link
Collaborator

r? @aturon

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Feb 6, 2017
@@ -380,6 +386,14 @@ impl Borrow<CStr> for CString {
fn borrow(&self) -> &CStr { self }
}

#[stable(feature = "box_from_slice", since = "1.17.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 think this should be unstable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Trait implementations are inherently stable, so, that's not an option.

@alexcrichton
Copy link
Member

I requested that this be split off #39438 (thanks @clarcharr!) because we don't have a lot of precedent for this in the standard library with other DSTs like Path, OsStr, etc. These seem reasonable to me, however, and I'd be fine adding all of them all at once as well.

@clarfonthey
Copy link
Contributor Author

I will add this for OsStr soon, although I need to find out a proper way to reconcile how the platform-specific types should be laid out. Path will come easily after that.

@clarfonthey clarfonthey changed the title Conversions between CStr, CString, and Box<CStr> Conversions between CStr, OsStr, Path and boxes Feb 8, 2017
@clarfonthey
Copy link
Contributor Author

I've added versions for OsStr and Path, and it'd be nice if someone took a quick glance over the code to make sure everything looks right. Because AppVeyor doesn't automatically do PRs at the moment, there might be a bug in the Windows code that won't be caught until bors tries to merge it.

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Feb 8, 2017

@aturon / @alexcrichton, I'll open up a tracking issue for the into_boxed_* methods once I get a nod that those methods are desired. This will be a bit of an odd change because the From impls will be immediately stable but the into_* methods will not be. If it makes more sense to open an RFC for this I can do that.

@alexcrichton
Copy link
Member

Nah yeah we can deal with the tracking issue just before merging, and these impls shouldn't require an RFC. Thanks for the updates!

@clarfonthey clarfonthey force-pushed the cstr_box branch 3 times, most recently from a878b3d to a0bb95c Compare February 10, 2017 06:01
@clarfonthey
Copy link
Contributor Author

Currently waiting on #39438 which, even though it said it was merged before, was not actually merged.

@clarfonthey clarfonthey force-pushed the cstr_box branch 3 times, most recently from f4db2cb to 6dbd0f8 Compare February 12, 2017 22:09
@clarfonthey
Copy link
Contributor Author

Waiting on CI, but I've implemented everything necessary for this and made a full list in the OP of what was implemented.

@clarfonthey clarfonthey force-pushed the cstr_box branch 3 times, most recently from 72f5273 to 9ed53cf Compare February 12, 2017 23:11
@clarfonthey
Copy link
Contributor Author

Ping @aturon

@aturon
Copy link
Member

aturon commented Feb 13, 2017

@clarcharr Thanks! This looks good to me.

@rust-lang/libs: This PR involves some instantly-stable trait implementations. However, everything being implemented here is filling out our inter-conversion story to match other types in std, which should make things more consistent and predictable. I will likely r+ soon, so if you have objections or want time to consider further, please speak up!

@aturon
Copy link
Member

aturon commented Feb 13, 2017

@clarcharr One thing that makes me a bit uncomfortable about the implementation here is the pervasive use of mem::transmute. I wonder if we can at least factor out some common conversions into some free-standing functions that can be re-used? Each transmute is basically making an assertion about the relative representations, and it'd be good to consolidate those if we can.

@BurntSushi
Copy link
Member

I think I'm fine with this.

In the implementation there are a lot of transmutes. Are they all necessary or is there anyway to get rid of some of them?

@BurntSushi
Copy link
Member

@aturon Haha. Beat me to it. :P

@alexcrichton
Copy link
Member

Looks good to me! I think we could annotate the input/output types in the transmutes, but otherwise I think they may unfortunately be required

@clarfonthey
Copy link
Contributor Author

The main issue with the transmutes is that there's no other way of converting newtype DSTs. I decided to do one type at a time which is why there are so many, because each transmute adds one more layer of newtypes.

It's most complicated in the instance of Path on Windows, which is [u8] -> Wtf8 -> Slice -> OsStr -> Path.

I could just coerce an empty u8 slice to a path, but that makes it harder to understand the conversion that's actually happening.

@aturon
Copy link
Member

aturon commented Feb 13, 2017

Alright, given that the transmutes appear necessary, let's go ahead and land this!

@bor: r+

@aturon
Copy link
Member

aturon commented Feb 13, 2017

@bors: r+

@bors
Copy link
Contributor

bors commented Feb 13, 2017

📌 Commit 9ed53cf has been approved by aturon

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 14, 2017
Conversions between CStr, OsStr, Path and boxes

This closes a bit of the inconsistencies between `CStr`, `OsStr`, `Path`, and `str`, allowing people to create boxed versions of DSTs other than `str` and `[T]`.

Full list of additions:
* `Default` for `Box<str>`, `Box<CStr>`, `Box<OsStr>`, and `Box<Path>` (note: `Default` for `PathBuf` is already implemented)
* `CString::into_boxed_c_str` (feature gated)
* `OsString::into_boxed_os_str` (feature gated)
* `Path::into_boxed_path` (feature gated)
* `From<&CStr> for Box<CStr>`
* `From<&OsStr> for Box<OsStr>`
* `From<&Path> for Box<Path>`

This also includes adding the internal methods:
* `sys::*::os_str::Buf::into_box`
* `sys::*::os_str::Slice::{into_box, empty_box}`
* `sys_common::wtf8::Wtf8Buf::into_box`
* `sys_common::wtf8::Wtf8::{into_box, empty_box}`
@frewsxcv
Copy link
Member

@clarfonthey
Copy link
Contributor Author

Should be working now.

@alexcrichton
Copy link
Member

@bors: r=aturon

@bors
Copy link
Contributor

bors commented Feb 14, 2017

📌 Commit 963843b has been approved by aturon

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Feb 15, 2017
Conversions between CStr, OsStr, Path and boxes

This closes a bit of the inconsistencies between `CStr`, `OsStr`, `Path`, and `str`, allowing people to create boxed versions of DSTs other than `str` and `[T]`.

Full list of additions:
* `Default` for `Box<str>`, `Box<CStr>`, `Box<OsStr>`, and `Box<Path>` (note: `Default` for `PathBuf` is already implemented)
* `CString::into_boxed_c_str` (feature gated)
* `OsString::into_boxed_os_str` (feature gated)
* `Path::into_boxed_path` (feature gated)
* `From<&CStr> for Box<CStr>`
* `From<&OsStr> for Box<OsStr>`
* `From<&Path> for Box<Path>`

This also includes adding the internal methods:
* `sys::*::os_str::Buf::into_box`
* `sys::*::os_str::Slice::{into_box, empty_box}`
* `sys_common::wtf8::Wtf8Buf::into_box`
* `sys_common::wtf8::Wtf8::{into_box, empty_box}`
@bors
Copy link
Contributor

bors commented Feb 15, 2017

⌛ Testing commit 963843b with merge e0044bd...

bors added a commit that referenced this pull request Feb 15, 2017
Conversions between CStr, OsStr, Path and boxes

This closes a bit of the inconsistencies between `CStr`, `OsStr`, `Path`, and `str`, allowing people to create boxed versions of DSTs other than `str` and `[T]`.

Full list of additions:
* `Default` for `Box<str>`, `Box<CStr>`, `Box<OsStr>`, and `Box<Path>` (note: `Default` for `PathBuf` is already implemented)
* `CString::into_boxed_c_str` (feature gated)
* `OsString::into_boxed_os_str` (feature gated)
* `Path::into_boxed_path` (feature gated)
* `From<&CStr> for Box<CStr>`
* `From<&OsStr> for Box<OsStr>`
* `From<&Path> for Box<Path>`

This also includes adding the internal methods:
* `sys::*::os_str::Buf::into_box`
* `sys::*::os_str::Slice::{into_box, empty_box}`
* `sys_common::wtf8::Wtf8Buf::into_box`
* `sys_common::wtf8::Wtf8::{into_box, empty_box}`
@bors
Copy link
Contributor

bors commented Feb 15, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: aturon
Pushing e0044bd to master...

@jonhoo
Copy link
Contributor

jonhoo commented Mar 4, 2017

Out of curiosity, why is into_boxed_c_str behind a feature flag for now?

@clarfonthey
Copy link
Contributor Author

@jonhoo, by protocol a tracking issue is opened for every new feature. So you'll have to wait until this goes stable for that to happen.

@clarfonthey
Copy link
Contributor Author

I also realised that I never made a tracking issue for this, so, feel free to open a PR to add one in so that this can be nominated faster.

bors added a commit that referenced this pull request Mar 15, 2017
Leftovers from #39594; From<Box> impls

These are a few more impls that follow the same reasoning as those from #39594.

What's included:
* `From<Box<str>> for String`
* `From<Box<[T]>> for Vec<T>`
* `From<Box<CStr>> for CString`
* `From<Box<OsStr>> for OsString`
* `From<Box<Path>> for PathBuf`
* `Into<Box<str>> for String`
* `Into<Box<[T]>> for Vec<T>`
* `Into<Box<CStr>> for CString`
* `Into<Box<OsStr>> for OsString`
* `Into<Box<Path>> for PathBuf`
* `<Box<CStr>>::into_c_string`
* `<Box<OsStr>>::into_os_string`
* `<Box<Path>>::into_path_buf`
* Tracking issue for latter three methods + three from previous PR.

Currently, the opposite direction isn't doable with `From` (only `Into`) because of the separation between `liballoc` and `libcollections`. I'm holding off on those for a later 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.

9 participants