about summary refs log tree commit diff
path: root/tvix/nix-compat/src/store_path/mod.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-04-12T09·38+0300
committerflokli <flokli@flokli.de>2024-04-13T10·03+0000
commit57fba1f167ac64ded89d4c61af08bb0f65c7f1f5 (patch)
tree89b453df34c20061085d1f99d38cc73c15cfedea /tvix/nix-compat/src/store_path/mod.rs
parentfd4c7c10a8eb32a5b8ed7f7cbdc991692a36c30a (diff)
feat(nix-compat/store_path): impl [Partial]Ord for StorePathRef r/7889
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
Diffstat (limited to 'tvix/nix-compat/src/store_path/mod.rs')
-rw-r--r--tvix/nix-compat/src/store_path/mod.rs31
1 files changed, 15 insertions, 16 deletions
diff --git a/tvix/nix-compat/src/store_path/mod.rs b/tvix/nix-compat/src/store_path/mod.rs
index 639bf4289ff6..a6dc74fb90f4 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<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