-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
impl AsRef<Path> for Cow<'_, str> #73390
Conversation
r? @dtolnay (rust_highfive has picked a reviewer for you, use r? to override) |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
There is already `impl AsRef<OsStr> for str` and `impl<'_> AsRef<Path> for Cow<'_, OsStr>` but one still need to do `s.as_ref()` for functions that takes in `AsRef<Path>` for `Cow<'_ str>` such as `Path::join()`.
0d6e618
to
3fd5141
Compare
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.
Could you provide a playground showing the code with Path::join where you hit the need for this?
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
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.
Cow<str> is a quite widely used type with currently one unique AsRef impl, so I think it would be too disruptive to do this. It breaks the following code:
use std::borrow::Cow;
fn main() {
let s = Cow::Borrowed("");
let _ = s.as_ref();
}
error[E0283]: type annotations needed
--> src/main.rs:5:15
|
5 | let _ = s.as_ref();
| ^^^^^^ cannot infer type for enum `std::borrow::Cow<'_, str>`
|
= note: cannot satisfy `std::borrow::Cow<'_, str>: std::convert::AsRef<_>`
Thanks anyway! |
But isn't that the case only when they use I believe the use of |
See the CI failures reported by rust-highfive. This breaks 2 places just in rustc_session so I believe it would break a significant fraction of crates.io where similar code is found. error[E0283]: type annotations needed
--> src/librustc_session/filesearch.rs:93:42
|
93 | p.push(find_libdir(self.sysroot).as_ref());
|
|
= note: cannot satisfy `std::borrow::Cow<'_, str>: std::convert::AsRef<_>`
error[E0283]: type annotations needed
--> src/librustc_session/filesearch.rs:102:52
|
|
102 | let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
|
|
= note: cannot satisfy `std::borrow::Cow<'_, str>: std::convert::AsRef<_>` |
The underlying reason for this problem is rooted deeper (in how |
…riplett docs: Improve AsRef / AsMut docs on blanket impls There are several issues with the current state of `AsRef` and `AsMut` as [discussed here on IRLO](https://internals.rust-lang.org/t/semantics-of-asref/17016). See also rust-lang#39397, rust-lang#45742, rust-lang#73390, rust-lang#98905, and the FIXMEs [here](https://github.com/rust-lang/rust/blob/1.62.0/library/core/src/convert/mod.rs#L509-L515) and [here](https://github.com/rust-lang/rust/blob/1.62.0/library/core/src/convert/mod.rs#L530-L536). These issues are difficult to fix. This PR aims to update the documentation to better reflect the status-quo and to give advice on how `AsRef` and `AsMut` should be used. In particular: - Explicitly mention that `AsRef` and `AsMut` do not auto-dereference generally for all dereferencable types (but only if inner type is a shared and/or mutable reference) - Give advice to not use `AsRef` or `AsMut` for the sole purpose of dereferencing - Suggest providing a transitive `AsRef` or `AsMut` implementation for types which implement `Deref` - Add new section "Reflexivity" in documentation comments for `AsRef` and `AsMut` - Provide better example for `AsMut` - Added heading "Relation to `Borrow`" in `AsRef`'s docs to improve structure
…riplett docs: Improve AsRef / AsMut docs on blanket impls There are several issues with the current state of `AsRef` and `AsMut` as [discussed here on IRLO](https://internals.rust-lang.org/t/semantics-of-asref/17016). See also rust-lang#39397, rust-lang#45742, rust-lang#73390, rust-lang#98905, and the FIXMEs [here](https://github.com/rust-lang/rust/blob/1.62.0/library/core/src/convert/mod.rs#L509-L515) and [here](https://github.com/rust-lang/rust/blob/1.62.0/library/core/src/convert/mod.rs#L530-L536). These issues are difficult to fix. This PR aims to update the documentation to better reflect the status-quo and to give advice on how `AsRef` and `AsMut` should be used. In particular: - Explicitly mention that `AsRef` and `AsMut` do not auto-dereference generally for all dereferencable types (but only if inner type is a shared and/or mutable reference) - Give advice to not use `AsRef` or `AsMut` for the sole purpose of dereferencing - Suggest providing a transitive `AsRef` or `AsMut` implementation for types which implement `Deref` - Add new section "Reflexivity" in documentation comments for `AsRef` and `AsMut` - Provide better example for `AsMut` - Added heading "Relation to `Borrow`" in `AsRef`'s docs to improve structure
…riplett docs: Improve AsRef / AsMut docs on blanket impls There are several issues with the current state of `AsRef` and `AsMut` as [discussed here on IRLO](https://internals.rust-lang.org/t/semantics-of-asref/17016). See also rust-lang#39397, rust-lang#45742, rust-lang#73390, rust-lang#98905, and the FIXMEs [here](https://github.com/rust-lang/rust/blob/1.62.0/library/core/src/convert/mod.rs#L509-L515) and [here](https://github.com/rust-lang/rust/blob/1.62.0/library/core/src/convert/mod.rs#L530-L536). These issues are difficult to fix. This PR aims to update the documentation to better reflect the status-quo and to give advice on how `AsRef` and `AsMut` should be used. In particular: - Explicitly mention that `AsRef` and `AsMut` do not auto-dereference generally for all dereferencable types (but only if inner type is a shared and/or mutable reference) - Give advice to not use `AsRef` or `AsMut` for the sole purpose of dereferencing - Suggest providing a transitive `AsRef` or `AsMut` implementation for types which implement `Deref` - Add new section "Reflexivity" in documentation comments for `AsRef` and `AsMut` - Provide better example for `AsMut` - Added heading "Relation to `Borrow`" in `AsRef`'s docs to improve structure
Maybe this should be reconsidered in some way, re "breakage of time crate" due to change in 1.80 recently: https://internals.rust-lang.org/t/type-inference-breakage-in-1-80-has-not-been-handled-well/21374/6 |
There is already
impl AsRef<OsStr> for str
andimpl<'_> AsRef<Path> for Cow<'_, OsStr>
but one still need to dos.as_ref()
for functions that takes inAsRef<Path>
forCow<'_ str>
such as
Path::join()
.