-
Notifications
You must be signed in to change notification settings - Fork 13k
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
docs: std::hash::Hash
should ensure prefix-free data
#89438
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -153,9 +153,19 @@ mod sip; | |
/// Thankfully, you won't need to worry about upholding this property when | ||
/// deriving both [`Eq`] and `Hash` with `#[derive(PartialEq, Eq, Hash)]`. | ||
/// | ||
/// ## Prefix collisions | ||
/// | ||
/// Implementations of `hash` should ensure that the data they | ||
/// pass to the `Hasher` are prefix-free. That is, different concatenations | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This explanation of what "prefix-free" means is incomplete. It should say that unequal values should cause two different byte sequences to be written, and neither of the two sequences should be a prefix of the other. Note that it's not sufficient to say that concatenations of outputs of multiple values of the same type should result in different outputs. It has to be true when concatenated with outputs for other types as well (think about hashing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @tczajka! I'm not sure I understand the idea of one sequence being a prefix of another. Does it simply mean "starts with", or is it another kind of relation? Is there a way we can rephrase this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another way to ask the question: in the example of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A "prefix" is a beginning of a string, so it's same as "starts with". https://en.wikipedia.org/wiki/Prefix If strings were hashed without the extra 0xff at the end, hashing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: |
||
/// of the same data should not produce the same output. | ||
/// For example, the standard implementation of [`Hash` for `&str`][impl] passes an extra | ||
/// `0xFF` byte to the `Hasher` so that the values `("ab", "c")` and `("a", | ||
/// "bc")` hash differently. | ||
/// | ||
/// [`HashMap`]: ../../std/collections/struct.HashMap.html | ||
/// [`HashSet`]: ../../std/collections/struct.HashSet.html | ||
/// [`hash`]: Hash::hash | ||
/// [impl]: ../../std/primitive.str.html#impl-Hash | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[rustc_diagnostic_item = "Hash"] | ||
pub trait Hash { | ||
|
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.
Thinking more about this... "Collision" isn't the right term, here, is it?
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.
It's not but I can't think of a better word to use.