Skip to content

Implement Hash for tuples of up to arity 12. Also change the style to be consistent with core::tuple #16033

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

Merged
merged 1 commit into from
Jul 29, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/libcollections/hash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,29 +155,36 @@ macro_rules! impl_hash_tuple(
}
);

($A:ident $($B:ident)*) => (
impl<
S: Writer,
$A: Hash<S> $(, $B: Hash<S>)*
> Hash<S> for ($A, $($B),*) {
( $($name:ident)+) => (
impl<S: Writer, $($name: Hash<S>),*> Hash<S> for ($($name,)*) {
#[allow(uppercase_variables)]
#[inline]
fn hash(&self, state: &mut S) {
match *self {
(ref $A, $(ref $B),*) => {
$A.hash(state);
($(ref $name,)*) => {
$(
$B.hash(state);
$name.hash(state);
)*
}
}
}
}

impl_hash_tuple!($($B)*)
);
)

impl_hash_tuple!(a0 a1 a2 a3 a4 a5 a6 a7)
impl_hash_tuple!()
impl_hash_tuple!(A)
impl_hash_tuple!(A B)
impl_hash_tuple!(A B C)
impl_hash_tuple!(A B C D)
impl_hash_tuple!(A B C D E)
impl_hash_tuple!(A B C D E F)
impl_hash_tuple!(A B C D E F G)
impl_hash_tuple!(A B C D E F G H)
impl_hash_tuple!(A B C D E F G H I)
impl_hash_tuple!(A B C D E F G H I J)
impl_hash_tuple!(A B C D E F G H I J K)
impl_hash_tuple!(A B C D E F G H I J K L)
Copy link
Member

Choose a reason for hiding this comment

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

Why move from the one line invocation to 12 lines of invocations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this is really silly, but with one line invocation and a recursive macro it ends up coming out like:

impl Trait for (A, B, C, D)
impl Trait for (B, C, D)
impl Trait for (C, D)
impl Trait for (D,)

I want it to come out

impl Trait for (A,)
impl Trait for (A, B)
impl Trait for (A, B, C)
impl Trait for (A, B, C, D)

Copy link
Member

Choose a reason for hiding this comment

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

In that case, could the recursive macro invocation be placed first?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That will help a bit, but it will still expand to

impl Trait for (D,)
impl Trait for (C, D)
impl Trait for (B, C, D)
impl Trait for (A, B, C, D)

I was mostly doing it so that the implementation for a 1-tuple would use "A", for a 2-tuple would use "A, B", for a 3-tuple would use "A, B, C", etc. Maybe this is too minor to worry about.

Copy link
Member

Choose a reason for hiding this comment

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

Oh well :(


impl<'a, S: Writer, T: Hash<S>> Hash<S> for &'a [T] {
#[inline]
Expand Down