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
HashMaps with tuple keys are not sorted, even with setting sort_maps set to true #302
Comments
Looking at the code, the issue seems to stem from insta/src/content/serialization.rs Line 53 in 89f2c2f
A tuple key has type I wonder if the items.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Less)); and nothing bad has happened yet. My motivating example however now passes, and the serialization produced by What do you think ? |
The motivation is that |
Thanks for your answer ! Ok, I see. Then the current code could simply be adapted by adding one more #[derive(PartialEq, PartialOrd, Debug)]
pub enum Key<'a> {
U64(u64),
I64(i64),
F64(f64),
U128(u128),
I128(i128),
Other(&'a Content),
}
impl<'a> Eq for Key<'a> {}
impl<'a> Ord for Key<'a> {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap_or(Ordering::Less)
}
}
impl Content {
pub(crate) fn as_key(&self) -> Key<'_> {
match self.resolve_inner() {
Content::Char(val) => Key::U64(*val as u64),
Content::U16(val) => Key::U64((*val).into()),
Content::U32(val) => Key::U64((*val).into()),
Content::U64(val) => Key::U64(*val),
Content::U128(val) => Key::U128(*val),
Content::I16(val) => Key::I64((*val).into()),
Content::I32(val) => Key::I64((*val).into()),
Content::I64(val) => Key::I64(*val),
Content::I128(val) => Key::I128(*val),
Content::F32(val) => Key::F64((*val).into()),
Content::F64(val) => Key::F64(*val),
other => Key::Other(other),
}
}
} (the |
Thanks for the fix :) |
What happened?
As the title says. This test should pass when triggered several times:
However, this test fails spuriously. Indeed, when reviewing the test result, one can easily see that the map is not sorted. One such result is the following:
Reproduction steps
No response
Insta Version
1.21
rustc Version
1.64
What did you expect?
HashMaps using tuples as key should be sorted before serialization if the appropriate setting is set.
The text was updated successfully, but these errors were encountered: