-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Conversions between CStr, OsStr, Path and boxes #39594
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
r? @aturon (rust_highfive has picked a reviewer for you, use r? to override) |
src/libstd/ffi/c_str.rs
Outdated
@@ -380,6 +386,14 @@ impl Borrow<CStr> for CString { | |||
fn borrow(&self) -> &CStr { self } | |||
} | |||
|
|||
#[stable(feature = "box_from_slice", since = "1.17.0")] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
I will add this for |
I've added versions for |
@aturon / @alexcrichton, I'll open up a tracking issue for the |
Nah yeah we can deal with the tracking issue just before merging, and these impls shouldn't require an RFC. Thanks for the updates! |
a878b3d
to
a0bb95c
Compare
Currently waiting on #39438 which, even though it said it was merged before, was not actually merged. |
f4db2cb
to
6dbd0f8
Compare
Waiting on CI, but I've implemented everything necessary for this and made a full list in the OP of what was implemented. |
72f5273
to
9ed53cf
Compare
Ping @aturon |
@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 |
@clarcharr One thing that makes me a bit uncomfortable about the implementation here is the pervasive use of |
I think I'm fine with this. In the implementation there are a lot of |
@aturon Haha. Beat me to it. :P |
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 |
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. |
Alright, given that the @bor: r+ |
@bors: r+ |
📌 Commit 9ed53cf has been approved by |
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 r- This fails to compile on Windows: https://ci.appveyor.com/project/rust-lang/rust/build/1.0.1944/job/cu5i3oo7m5dq2hbg |
9ed53cf
to
963843b
Compare
Should be working now. |
@bors: r=aturon |
📌 Commit 963843b has been approved by |
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}`
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}`
☀️ Test successful - status-appveyor, status-travis |
Out of curiosity, why is |
@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. |
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. |
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.
This closes a bit of the inconsistencies between
CStr
,OsStr
,Path
, andstr
, allowing people to create boxed versions of DSTs other thanstr
and[T]
.Full list of additions:
Default
forBox<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}