Implement TryFrom<OsString> for String#151822
Conversation
This comment has been minimized.
This comment has been minimized.
354b70a to
dec3d5b
Compare
This comment has been minimized.
This comment has been minimized.
c73b8e0 to
68ea3bf
Compare
This comment has been minimized.
This comment has been minimized.
68ea3bf to
2ff5e55
Compare
This comment has been minimized.
This comment has been minimized.
Being able to generically convert strings can be beneficial for argument parsing code and similar situations especially in case of conditional conversions. The standard library already provided this converion, just not via a trait. This commit fills the gap by adding the impl. This addition was approved in [ACP 732]. It was requested that `FromUtf8Error` should be made generic over the input and this commit obeys the request. However some challenges were encountered: * The fields were private and the type had no constructor - solved by making the fields public but unstable and `#[doc(hidden)]`. * There is a method to perform lossy conversion and it looks like it should be provided for `OsString` as well - this is not yet resolved. * `into_os_string` method was requested but the types are in different crates - solved with `#[rustc_allow_incoherent_impl]`. [ACP 732]: rust-lang/libs-team#732
2ff5e55 to
3a3227c
Compare
|
Kicking off a build for crater. @bors try |
This comment has been minimized.
This comment has been minimized.
Implement `TryFrom<OsString>` for `String`
|
Since you have already been thinking about the design in the ACP, r? programmerjake |
| /// Returns an [`OsStr`] slice that was attempted to convert to a `String`. | ||
| #[stable(feature = "tryfrom_os_string_for_string", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_allow_incoherent_impl] | ||
| pub fn as_os_str(&self) -> &OsStr { | ||
| &self.input[..] | ||
| } | ||
|
|
||
| /// Returns the [`OsString`] that was attempted to convert to a `String`. | ||
| /// | ||
| /// This method is carefully constructed to avoid allocation. It will | ||
| /// consume the error, moving out the string, so that a copy of the string | ||
| /// does not need to be made. | ||
| #[stable(feature = "tryfrom_os_string_for_string", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_allow_incoherent_impl] | ||
| pub fn into_os_string(self) -> OsString { | ||
| self.input | ||
| } |
There was a problem hiding this comment.
These can be added unstably (unlike traits). I think it will probably be fine to put up a stabilization PR shortly after this merges.
There was a problem hiding this comment.
technically trait impls can be unstable too (afaik they actually work now), but idk if it's standard practice to use that yet.
There was a problem hiding this comment.
🎉 that's news to me! I think we may as well try it
There was a problem hiding this comment.
see https://rustc-dev-guide.rust-lang.org/stability.html#unstable_feature_bound for docs.
You'll need to create a tracking issue, and replace the below NNN with that issue number.
#[unstable(feature = "tryfrom_os_string_for_string", issue = "NNN")]
#[unstable_feature_bound(tryfrom_os_string_for_string)]
impl TryFrom<OsString> for String {
...
}There was a problem hiding this comment.
also, you'll want to add #[must_use = ...] to those methods to match the methods on FromUtf8Error<Vec<u8>>
|
|
||
| /// Attempts to convert an [`OsString`] into a [`String`]. | ||
| /// | ||
| /// This conversion does not allocate or copy memory. |
There was a problem hiding this comment.
It does technically copy memory, just not the contents of the OsString.
The docs should probably be based on the docs of OsString::into_string:
rust/library/std/src/ffi/os_str.rs
Lines 227 to 239 in 36e2b8a
There was a problem hiding this comment.
Right, I copied it from some other method that says the same thing in such scenario. I will look into it.
| /// Returns an [`OsStr`] slice that was attempted to convert to a `String`. | ||
| #[stable(feature = "tryfrom_os_string_for_string", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_allow_incoherent_impl] | ||
| pub fn as_os_str(&self) -> &OsStr { | ||
| &self.input[..] | ||
| } | ||
|
|
||
| /// Returns the [`OsString`] that was attempted to convert to a `String`. | ||
| /// | ||
| /// This method is carefully constructed to avoid allocation. It will | ||
| /// consume the error, moving out the string, so that a copy of the string | ||
| /// does not need to be made. | ||
| #[stable(feature = "tryfrom_os_string_for_string", since = "CURRENT_RUSTC_VERSION")] | ||
| #[rustc_allow_incoherent_impl] | ||
| pub fn into_os_string(self) -> OsString { | ||
| self.input | ||
| } |
There was a problem hiding this comment.
also, you'll want to add #[must_use = ...] to those methods to match the methods on FromUtf8Error<Vec<u8>>
|
So, as mentioned, please change the API to @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
|
@craterbot check |
|
👌 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
|
🎉 Experiment
Footnotes
|
|
looks like nothing significant changed in the crater run. so I'm changing this back to waiting on the author: #151822 (comment) @rustbot author |
This is draft because I was unable to compile locally, it will be probably broken for a few iterations.
Being able to generically convert strings can be beneficial for argument parsing code and similar situations especially in case of conditional conversions. The standard library already provided this converion, just not via a trait. This commit fills the gap by adding the impl.
This addition was approved in ACP 732. It was requested that
FromUtf8Errorshould be made generic over the input and this commit obeys the request. However some challenges were encountered:#[doc(hidden)].OsStringas well - this is not yet resolved.into_os_stringmethod was requested but the types are in different crates - solved with#[rustc_allow_incoherent_impl].