feat(nix-compat/store_path): impl [Partial]Ord for StorePathRef

Move the code implementing it from StorePath to StorePathRef, and have
the StorePath impls use that too.

Drop the debug_assert in every comparison - we have tests for this to
ensure it keeps working, and built up some confidence by piping a lot of
other store paths through it in the meantime.

Change-Id: I288bad3dfa597f68d63c4bcda7791f722b7a8ced
Reviewed-on: https://cl.tvl.fyi/c/depot/+/11392
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Tested-by: BuildkiteCI
This commit is contained in:
Florian Klink 2024-04-12 12:38:15 +03:00 committed by flokli
parent fd4c7c10a8
commit 57fba1f167

View file

@ -86,22 +86,7 @@ impl PartialOrd for StorePath {
/// of the nixbase32-encoded string.
impl Ord for StorePath {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
let order = self.digest.iter().rev().cmp(other.digest.iter().rev());
// This order must match the order of the nixbase32 encoded digests.
#[cfg(debug_assertions)]
{
let self_hash = nixbase32::encode(&self.digest);
let other_hash = nixbase32::encode(&other.digest);
let canonical_order = self_hash.cmp(&other_hash);
assert_eq!(
order, canonical_order,
"Ordering of nixbase32 differs, {:?} instead of {:?}:\n{:?}\n{:?}",
order, canonical_order, self_hash, other_hash
);
}
order
self.as_ref().cmp(&other.as_ref())
}
}
@ -198,6 +183,20 @@ impl<'a> From<&'a StorePath> for StorePathRef<'a> {
}
}
impl<'a> PartialOrd for StorePathRef<'a> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
/// `StorePathRef`s are sorted by their reverse digest to match the sorting order
/// of the nixbase32-encoded string.
impl<'a> Ord for StorePathRef<'a> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.digest.iter().rev().cmp(other.digest.iter().rev())
}
}
impl<'a> StorePathRef<'a> {
pub fn digest(&self) -> &[u8; DIGEST_SIZE] {
&self.digest