Skip to content

Commit

Permalink
fix(nix-compat/nixbase32): mark encode_len/decode_len const
Browse files Browse the repository at this point in the history
Change-Id: Ib688bbb37cd54cfcd01e5cb3a8c376414ee8311e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9926
Reviewed-by: flokli <flokli@flokli.de>
Tested-by: BuildkiteCI
  • Loading branch information
edef1c committed Nov 4, 2023
1 parent fed8854 commit 04a25b5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
4 changes: 2 additions & 2 deletions nix-compat/src/nixbase32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ fn find_invalid(input: &[u8]) -> u8 {
}

/// Returns the decoded length of an input of length len.
pub fn decode_len(len: usize) -> usize {
pub const fn decode_len(len: usize) -> usize {
(len * 5) / 8
}

/// Returns the encoded length of an input of length len
pub fn encode_len(len: usize) -> usize {
pub const fn encode_len(len: usize) -> usize {
(len * 8 + 4) / 5
}

Expand Down
9 changes: 1 addition & 8 deletions nix-compat/src/store_path/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ mod utils;
pub use utils::*;

pub const DIGEST_SIZE: usize = 20;
// lazy_static doesn't allow us to call NIXBASE32.encode_len(), so we ran it
// manually and have an assert in the tests.
pub const ENCODED_DIGEST_SIZE: usize = 32;
pub const ENCODED_DIGEST_SIZE: usize = nixbase32::encode_len(DIGEST_SIZE);

// The store dir prefix, without trailing slash.
// That's usually where the Nix store is mounted at.
Expand Down Expand Up @@ -298,11 +296,6 @@ mod tests {

use super::{Error, StorePath};

#[test]
fn encoded_digest_size() {
assert_eq!(ENCODED_DIGEST_SIZE, nixbase32::encode_len(DIGEST_SIZE));
}

#[test]
fn happy_path() {
let example_nix_path_str =
Expand Down

0 comments on commit 04a25b5

Please sign in to comment.