-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Implement PartialOrd and PartialEq for UTF-8 strings and FFI Strings #76189
Implement PartialOrd and PartialEq for UTF-8 strings and FFI Strings #76189
Conversation
CI failed on |
This PR adds many more `PartialEq` and `PartialOrd` implementations for `Path`, `PathBuf`, `OsStr`, `OsString`, and `Component`. The Rust Standard Library guarantees that these types are freely and cheaply constructable from UTF-8 `String` and `&str`, but comparing these types with `&str` literals is not ergonomic. One use case I'd like to enable is filtering out paths containing a "test" directory while walking a tree and inspecting the path with the `components()` iterator. Example: ```rust let is_test_source = relative_source .components() .any(|component| component.as_os_str() == OsStr::new("test")); ``` New core trait impls: `OsString`: - `impl PartialEq<String> for OsString` and reflexive - `impl PartialEq<Cow<'_, str>> for OsString` and reflexive - `impl PartialEq<Box<str>> for OsString` and reflexive - `impl PartialEq<Rc<str>> for OsString` and reflexive - `impl PartialEq<Arc<str>> for OsString` and reflexive - `impl PartialOrd<String> for OsString` and reflexive - `impl PartialOrd<Cow<'_, str>> for OsString` and reflexive - `impl PartialOrd<Box<str>> for OsString` and reflexive - `impl PartialOrd<Rc<str>> for OsString` and reflexive - `impl PartialOrd<Arc<str>> for OsString` and reflexive `OsStr`: - `impl PartialEq<String> for OsStr` and reflexive - `impl PartialEq<Cow<'_, str>> for OsStr` and reflexive - `impl PartialEq<Box<str>> for OsStr` and reflexive - `impl PartialEq<Rc<str>> for OsStr` and reflexive - `impl PartialEq<Arc<str>> for OsStr` and reflexive - `impl PartialOrd<String> for OsStr` and reflexive - `impl PartialOrd<Cow<'_, str>> for OsStr` and reflexive - `impl PartialOrd<Box<str>> for OsStr` and reflexive - `impl PartialOrd<Rc<str>> for OsStr` and reflexive - `impl PartialOrd<Arc<str>> for OsStr` and reflexive `Component`: - `impl PartialEq<OsStr> for Component` and reflexive - `impl PartialEq<&'_ OsStr> for Component` and reflexive - `impl PartialEq<OsString> for Component` and reflexive - `impl PartialEq<Cow<'_, OsStr>> for Component` and reflexive - `impl PartialEq<Box<OsStr>> for Component` and reflexive - `impl PartialEq<Path> for Component` and reflexive - `impl PartialEq<&'_ Path> for Component` and reflexive - `impl PartialEq<PathBuf> for Component` and reflexive - `impl PartialEq<Cow<'_, Path>> for Component` and reflexive - `impl PartialEq<Box<Path>> for Component` and reflexive - `impl PartialEq<str> for Component` and reflexive - `impl PartialEq<String> for Component` and reflexive - `impl PartialEq<Cow<'_, str>> for Component` and reflexive - `impl PartialEq<Box<str>> for Component` and reflexive - `impl PartialEq<Rc<str>> for Component` and reflexive - `impl PartialEq<Arc<str>> for Component` and reflexive - `impl PartialEq<str> for Component` and reflexive - `impl PartialEq<String> for Component` and reflexive - `impl PartialEq<Cow<'_, str>> for Component` and reflexive - `impl PartialEq<Box<str>> for Component` and reflexive - `impl PartialEq<Rc<str>> for Component` and reflexive - `impl PartialEq<Arc<str>> for Component` and reflexive - `impl PartialOrd<OsStr> for Component` and reflexive - `impl PartialOrd<&'_ OsStr> for Component` and reflexive - `impl PartialOrd<OsString> for Component` and reflexive - `impl PartialOrd<Cow<'_, OsStr>> for Component` and reflexive - `impl PartialOrd<Box<OsStr>> for Component` and reflexive - `impl PartialOrd<Path> for Component` and reflexive - `impl PartialOrd<&'_ Path> for Component` and reflexive - `impl PartialOrd<PathBuf> for Component` and reflexive - `impl PartialOrd<Cow<'_, Path>> for Component` and reflexive - `impl PartialOrd<Box<Path>> for Component` and reflexive - `impl PartialOrd<str> for Component` and reflexive - `impl PartialOrd<String> for Component` and reflexive - `impl PartialOrd<Cow<'_, str>> for Component` and reflexive - `impl PartialOrd<Box<str>> for Component` and reflexive - `impl PartialOrd<Rc<str>> for Component` and reflexive - `impl PartialOrd<Arc<str>> for Component` and reflexive `PathBuf`: - `impl PartialEq<str> for PathBuf` and reflexive - `impl PartialEq<&'_ str> for PathBuf` and reflexive - `impl PartialEq<String> for PathBuf` and reflexive - `impl PartialEq<Cow<'_, str>> for PathBuf` and reflexive - `impl PartialEq<Box<str>> for PathBuf` and reflexive - `impl PartialEq<Rc<str>> for PathBuf` and reflexive - `impl PartialEq<Arc<str>> for PathBuf` and reflexive - `impl PartialOrd<str> for PathBuf` and reflexive - `impl PartialOrd<&'_ str> for PathBuf` and reflexive - `impl PartialOrd<String> for PathBuf` and reflexive - `impl PartialOrd<Cow<'_, str>> for PathBuf` and reflexive - `impl PartialOrd<Box<str>> for PathBuf` and reflexive - `impl PartialOrd<Rc<str>> for PathBuf` and reflexive - `impl PartialOrd<Arc<str>> for PathBuf` and reflexive `Path`: - `impl PartialEq<str> for Path` and reflexive - `impl PartialEq<&'_ str> for Path` and reflexive - `impl PartialEq<String> for Path` and reflexive - `impl PartialEq<Cow<'_, str>> for Path` and reflexive - `impl PartialEq<Box<str>> for Path` and reflexive - `impl PartialEq<Rc<str>> for Path` and reflexive - `impl PartialEq<Arc<str>> for Path` and reflexive - `impl PartialOrd<str> for Path` and reflexive - `impl PartialOrd<&'_ str> for Path` and reflexive - `impl PartialOrd<String> for Path` and reflexive - `impl PartialOrd<Cow<'_, str>> for Path` and reflexive - `impl PartialOrd<Box<str>> for Path` and reflexive - `impl PartialOrd<Rc<str>> for Path` and reflexive - `impl PartialOrd<Arc<str>> for Path` and reflexive Fixes #71700.
Added more |
It looks like this PR changes type inference. I'm not sure what to do next.
|
I believe this will require a libs team FCP? |
Those type inference errors are the problems that adding more impls like these cause. If it's breaking something in the compiler, there's a good chance it's going to break quite a bit of the ecosystem as well. |
I don't think this can be accepted as is, because it can cause breakage in the ecosystem. I'm going to close this for now to prevent it from rotting further, if you have a better way of achieving this, you are free to submit a new PR. Thanks for contributing :) |
Implement PartialOrd and PartialEq for UTF-8 strings and FFI Strings
This PR adds many more
PartialEq
andPartialOrd
implementations forPath
,PathBuf
,OsStr
,OsString
, andComponent
.The Rust Standard Library guarantees that these types are freely and
cheaply constructable from UTF-8
String
and&str
, but comparingthese types with
&str
literals is not ergonomic. One use case I'd liketo enable is filtering out paths containing a "test" directory while
walking a tree and inspecting the path with the
components()
iterator.Example:
New core trait impls:
OsString
:impl PartialEq<String> for OsString
and reflexiveimpl PartialEq<Cow<'_, str>> for OsString
and reflexiveimpl PartialEq<Box<str>> for OsString
and reflexiveimpl PartialEq<Rc<str>> for OsString
and reflexiveimpl PartialEq<Arc<str>> for OsString
and reflexiveimpl PartialOrd<String> for OsString
and reflexiveimpl PartialOrd<Cow<'_, str>> for OsString
and reflexiveimpl PartialOrd<Box<str>> for OsString
and reflexiveimpl PartialOrd<Rc<str>> for OsString
and reflexiveimpl PartialOrd<Arc<str>> for OsString
and reflexiveOsStr
:impl PartialEq<String> for OsStr
and reflexiveimpl PartialEq<Cow<'_, str>> for OsStr
and reflexiveimpl PartialEq<Box<str>> for OsStr
and reflexiveimpl PartialEq<Rc<str>> for OsStr
and reflexiveimpl PartialEq<Arc<str>> for OsStr
and reflexiveimpl PartialOrd<String> for OsStr
and reflexiveimpl PartialOrd<Cow<'_, str>> for OsStr
and reflexiveimpl PartialOrd<Box<str>> for OsStr
and reflexiveimpl PartialOrd<Rc<str>> for OsStr
and reflexiveimpl PartialOrd<Arc<str>> for OsStr
and reflexiveComponent
:impl PartialEq<OsStr> for Component
and reflexiveimpl PartialEq<&'_ OsStr> for Component
and reflexiveimpl PartialEq<OsString> for Component
and reflexiveimpl PartialEq<Cow<'_, OsStr>> for Component
and reflexiveimpl PartialEq<Box<OsStr>> for Component
and reflexiveimpl PartialEq<Path> for Component
and reflexiveimpl PartialEq<&'_ Path> for Component
and reflexiveimpl PartialEq<PathBuf> for Component
and reflexiveimpl PartialEq<Cow<'_, Path>> for Component
and reflexiveimpl PartialEq<Box<Path>> for Component
and reflexiveimpl PartialEq<str> for Component
and reflexiveimpl PartialEq<String> for Component
and reflexiveimpl PartialEq<Cow<'_, str>> for Component
and reflexiveimpl PartialEq<Box<str>> for Component
and reflexiveimpl PartialEq<Rc<str>> for Component
and reflexiveimpl PartialEq<Arc<str>> for Component
and reflexiveimpl PartialEq<str> for Component
and reflexiveimpl PartialEq<String> for Component
and reflexiveimpl PartialEq<Cow<'_, str>> for Component
and reflexiveimpl PartialEq<Box<str>> for Component
and reflexiveimpl PartialEq<Rc<str>> for Component
and reflexiveimpl PartialEq<Arc<str>> for Component
and reflexiveimpl PartialOrd<OsStr> for Component
and reflexiveimpl PartialOrd<&'_ OsStr> for Component
and reflexiveimpl PartialOrd<OsString> for Component
and reflexiveimpl PartialOrd<Cow<'_, OsStr>> for Component
and reflexiveimpl PartialOrd<Box<OsStr>> for Component
and reflexiveimpl PartialOrd<Path> for Component
and reflexiveimpl PartialOrd<&'_ Path> for Component
and reflexiveimpl PartialOrd<PathBuf> for Component
and reflexiveimpl PartialOrd<Cow<'_, Path>> for Component
and reflexiveimpl PartialOrd<Box<Path>> for Component
and reflexiveimpl PartialOrd<str> for Component
and reflexiveimpl PartialOrd<String> for Component
and reflexiveimpl PartialOrd<Cow<'_, str>> for Component
and reflexiveimpl PartialOrd<Box<str>> for Component
and reflexiveimpl PartialOrd<Rc<str>> for Component
and reflexiveimpl PartialOrd<Arc<str>> for Component
and reflexivePathBuf
:impl PartialEq<str> for PathBuf
and reflexiveimpl PartialEq<&'_ str> for PathBuf
and reflexiveimpl PartialEq<String> for PathBuf
and reflexiveimpl PartialEq<Cow<'_, str>> for PathBuf
and reflexiveimpl PartialEq<Box<str>> for PathBuf
and reflexiveimpl PartialEq<Rc<str>> for PathBuf
and reflexiveimpl PartialEq<Arc<str>> for PathBuf
and reflexiveimpl PartialOrd<str> for PathBuf
and reflexiveimpl PartialOrd<&'_ str> for PathBuf
and reflexiveimpl PartialOrd<String> for PathBuf
and reflexiveimpl PartialOrd<Cow<'_, str>> for PathBuf
and reflexiveimpl PartialOrd<Box<str>> for PathBuf
and reflexiveimpl PartialOrd<Rc<str>> for PathBuf
and reflexiveimpl PartialOrd<Arc<str>> for PathBuf
and reflexivePath
:impl PartialEq<str> for Path
and reflexiveimpl PartialEq<&'_ str> for Path
and reflexiveimpl PartialEq<String> for Path
and reflexiveimpl PartialEq<Cow<'_, str>> for Path
and reflexiveimpl PartialEq<Box<str>> for Path
and reflexiveimpl PartialEq<Rc<str>> for Path
and reflexiveimpl PartialEq<Arc<str>> for Path
and reflexiveimpl PartialOrd<str> for Path
and reflexiveimpl PartialOrd<&'_ str> for Path
and reflexiveimpl PartialOrd<String> for Path
and reflexiveimpl PartialOrd<Cow<'_, str>> for Path
and reflexiveimpl PartialOrd<Box<str>> for Path
and reflexiveimpl PartialOrd<Rc<str>> for Path
and reflexiveimpl PartialOrd<Arc<str>> for Path
and reflexiveFixes #71700.