From 57fba1f167ac64ded89d4c61af08bb0f65c7f1f5 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Fri, 12 Apr 2024 12:38:15 +0300 Subject: 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 Tested-by: BuildkiteCI --- tvix/nix-compat/src/store_path/mod.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'tvix/nix-compat/src/store_path/mod.rs') diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs index 639bf4289f..a6dc74fb90 100644 --- a/tvix/nix-compat/src/store_path/mod.rs +++ b/tvix/nix-compat/src/store_path/mod.rs @@ -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 { + 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 -- cgit 1.4.1