Skip to content
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 Eq, Ord, PartialOrd, Hash for CxxString #743

Merged
merged 1 commit into from
Mar 18, 2021
Merged

Implement Eq, Ord, PartialOrd, Hash for CxxString #743

merged 1 commit into from
Mar 18, 2021

Conversation

jovenlin0527
Copy link
Contributor

Closes #737.

This is implemented by delegation to &[u8], as suggested.

I also implemented PartialOrd<str> for CxxString and PartialOrd<CxxString> for str, since PartialEq<str> is already implemented for CxxString and vice versa. I added tests for comparisons between str and CxxString, just in case.

@jovenlin0527
Copy link
Contributor Author

jovenlin0527 commented Mar 18, 2021

Oh by the way the standard library implements Hash for str differently. https://doc.rust-lang.org/src/core/hash/mod.rs.html#598-603. They add additional 0xff when hashing.
Do we follow the standard library? If we don't, then we should reconsider implementing PartialEq<str> for CxxStrins and some other traits.

@dtolnay dtolnay changed the title implment Ord, PartialOrd and Hash for CxxString. Implement Eq, Ord, PartialOrd, Hash for CxxString Mar 18, 2021
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@dtolnay dtolnay merged commit 3898878 into dtolnay:master Mar 18, 2021
@dtolnay
Copy link
Owner

dtolnay commented Mar 18, 2021

If we don't, then we should reconsider implementing PartialEq<str> for CxxString and some other traits.

Why? I don't know of any relationship that PartialEq would impose on the hash implementation.

@jovenlin0527
Copy link
Contributor Author

If we don't, then we should reconsider implementing PartialEq<str> for CxxString and some other traits.

Why? I don't know of any relationship that PartialEq would impose on the hash implementation.

I thought if two objects are equal, then they should have same hash? I may be mistaken though.

@dtolnay
Copy link
Owner

dtolnay commented Mar 18, 2021

That is definitely not required.

@jovenlin0527 jovenlin0527 deleted the cxx_string_traits branch March 18, 2021 20:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Trait impls for CxxString: Hash, Ord, PartialOrd, Eq
2 participants