about summary refs log tree commit diff
diff options
context:
space:
mode:
authoredef <edef@edef.eu>2023-10-27T00·01+0000
committeredef <edef@edef.eu>2023-10-27T07·31+0000
commit4d35a56798e8f33d758582c10a82659ac77de7ca (patch)
treeebb122df9cc21d6f695db592b5b4d93712418f37
parentb916c2d0b1ebf1239a1a2a54d0270f91487e5ce0 (diff)
refactor(tvix/nix-compat): simplify encode_len r/6880
Change-Id: Idfefd3b5b0d1ffbd4e9208e634a2d9989965a291
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9845
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
-rw-r--r--tvix/nix-compat/src/nixbase32.rs11
1 files changed, 5 insertions, 6 deletions
diff --git a/tvix/nix-compat/src/nixbase32.rs b/tvix/nix-compat/src/nixbase32.rs
index 7fef1d673e2f..86d900032d1a 100644
--- a/tvix/nix-compat/src/nixbase32.rs
+++ b/tvix/nix-compat/src/nixbase32.rs
@@ -104,10 +104,7 @@ pub fn decode_len(len: usize) -> usize {
 
 /// Returns the encoded length of an input of length len
 pub fn encode_len(len: usize) -> usize {
-    if len == 0 {
-        return 0;
-    }
-    (len * 8 - 1) / 5 + 1
+    (len * 8 + 4) / 5
 }
 
 #[cfg(test)]
@@ -157,11 +154,13 @@ mod tests {
 
     #[test]
     fn encode_len() {
-        assert_eq!(super::encode_len(20), 32)
+        assert_eq!(super::encode_len(0), 0);
+        assert_eq!(super::encode_len(20), 32);
     }
 
     #[test]
     fn decode_len() {
-        assert_eq!(super::decode_len(32), 20)
+        assert_eq!(super::decode_len(0), 0);
+        assert_eq!(super::decode_len(32), 20);
     }
 }