- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
Implement PartialOrd and PartialEq for UTF-8 strings and FFI Strings #76189
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
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
PartialEqandPartialOrdimplementations forPath,PathBuf,OsStr,OsString, andComponent.The Rust Standard Library guarantees that these types are freely and
cheaply constructable from UTF-8
Stringand&str, but comparingthese types with
&strliterals 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 OsStringand reflexiveimpl PartialEq<Cow<'_, str>> for OsStringand reflexiveimpl PartialEq<Box<str>> for OsStringand reflexiveimpl PartialEq<Rc<str>> for OsStringand reflexiveimpl PartialEq<Arc<str>> for OsStringand reflexiveimpl PartialOrd<String> for OsStringand reflexiveimpl PartialOrd<Cow<'_, str>> for OsStringand reflexiveimpl PartialOrd<Box<str>> for OsStringand reflexiveimpl PartialOrd<Rc<str>> for OsStringand reflexiveimpl PartialOrd<Arc<str>> for OsStringand reflexiveOsStr:impl PartialEq<String> for OsStrand reflexiveimpl PartialEq<Cow<'_, str>> for OsStrand reflexiveimpl PartialEq<Box<str>> for OsStrand reflexiveimpl PartialEq<Rc<str>> for OsStrand reflexiveimpl PartialEq<Arc<str>> for OsStrand reflexiveimpl PartialOrd<String> for OsStrand reflexiveimpl PartialOrd<Cow<'_, str>> for OsStrand reflexiveimpl PartialOrd<Box<str>> for OsStrand reflexiveimpl PartialOrd<Rc<str>> for OsStrand reflexiveimpl PartialOrd<Arc<str>> for OsStrand reflexiveComponent:impl PartialEq<OsStr> for Componentand reflexiveimpl PartialEq<&'_ OsStr> for Componentand reflexiveimpl PartialEq<OsString> for Componentand reflexiveimpl PartialEq<Cow<'_, OsStr>> for Componentand reflexiveimpl PartialEq<Box<OsStr>> for Componentand reflexiveimpl PartialEq<Path> for Componentand reflexiveimpl PartialEq<&'_ Path> for Componentand reflexiveimpl PartialEq<PathBuf> for Componentand reflexiveimpl PartialEq<Cow<'_, Path>> for Componentand reflexiveimpl PartialEq<Box<Path>> for Componentand reflexiveimpl PartialEq<str> for Componentand reflexiveimpl PartialEq<String> for Componentand reflexiveimpl PartialEq<Cow<'_, str>> for Componentand reflexiveimpl PartialEq<Box<str>> for Componentand reflexiveimpl PartialEq<Rc<str>> for Componentand reflexiveimpl PartialEq<Arc<str>> for Componentand reflexiveimpl PartialEq<str> for Componentand reflexiveimpl PartialEq<String> for Componentand reflexiveimpl PartialEq<Cow<'_, str>> for Componentand reflexiveimpl PartialEq<Box<str>> for Componentand reflexiveimpl PartialEq<Rc<str>> for Componentand reflexiveimpl PartialEq<Arc<str>> for Componentand reflexiveimpl PartialOrd<OsStr> for Componentand reflexiveimpl PartialOrd<&'_ OsStr> for Componentand reflexiveimpl PartialOrd<OsString> for Componentand reflexiveimpl PartialOrd<Cow<'_, OsStr>> for Componentand reflexiveimpl PartialOrd<Box<OsStr>> for Componentand reflexiveimpl PartialOrd<Path> for Componentand reflexiveimpl PartialOrd<&'_ Path> for Componentand reflexiveimpl PartialOrd<PathBuf> for Componentand reflexiveimpl PartialOrd<Cow<'_, Path>> for Componentand reflexiveimpl PartialOrd<Box<Path>> for Componentand reflexiveimpl PartialOrd<str> for Componentand reflexiveimpl PartialOrd<String> for Componentand reflexiveimpl PartialOrd<Cow<'_, str>> for Componentand reflexiveimpl PartialOrd<Box<str>> for Componentand reflexiveimpl PartialOrd<Rc<str>> for Componentand reflexiveimpl PartialOrd<Arc<str>> for Componentand reflexivePathBuf:impl PartialEq<str> for PathBufand reflexiveimpl PartialEq<&'_ str> for PathBufand reflexiveimpl PartialEq<String> for PathBufand reflexiveimpl PartialEq<Cow<'_, str>> for PathBufand reflexiveimpl PartialEq<Box<str>> for PathBufand reflexiveimpl PartialEq<Rc<str>> for PathBufand reflexiveimpl PartialEq<Arc<str>> for PathBufand reflexiveimpl PartialOrd<str> for PathBufand reflexiveimpl PartialOrd<&'_ str> for PathBufand reflexiveimpl PartialOrd<String> for PathBufand reflexiveimpl PartialOrd<Cow<'_, str>> for PathBufand reflexiveimpl PartialOrd<Box<str>> for PathBufand reflexiveimpl PartialOrd<Rc<str>> for PathBufand reflexiveimpl PartialOrd<Arc<str>> for PathBufand reflexivePath:impl PartialEq<str> for Pathand reflexiveimpl PartialEq<&'_ str> for Pathand reflexiveimpl PartialEq<String> for Pathand reflexiveimpl PartialEq<Cow<'_, str>> for Pathand reflexiveimpl PartialEq<Box<str>> for Pathand reflexiveimpl PartialEq<Rc<str>> for Pathand reflexiveimpl PartialEq<Arc<str>> for Pathand reflexiveimpl PartialOrd<str> for Pathand reflexiveimpl PartialOrd<&'_ str> for Pathand reflexiveimpl PartialOrd<String> for Pathand reflexiveimpl PartialOrd<Cow<'_, str>> for Pathand reflexiveimpl PartialOrd<Box<str>> for Pathand reflexiveimpl PartialOrd<Rc<str>> for Pathand reflexiveimpl PartialOrd<Arc<str>> for Pathand reflexiveFixes #71700.