diff options
author | Florian Klink <flokli@flokli.de> | 2024-10-13T15·31+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2024-10-17T21·17+0000 |
commit | cdbdd2d04ecb2675f9c1d7ded7d99ad3a483b6fc (patch) | |
tree | 83c68d68e44e3b056213c58e4c9f03e18a1fb860 /tvix/castore/src/fixtures.rs | |
parent | a94414e7ffc5c21874fa0ea6e92aa63fcc55cb7d (diff) |
refactor(tvix/castore): remove use of lazy_static r/8823
This is now supported in the standard library via std::sync::LazyLock, but requires some manual shuffling around of code. Change-Id: Ia0370ca46cb1c6122a452b1d117160536b632c7e Reviewed-on: https://cl.tvl.fyi/c/depot/+/12612 Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/castore/src/fixtures.rs')
-rw-r--r-- | tvix/castore/src/fixtures.rs | 142 |
1 files changed, 79 insertions, 63 deletions
diff --git a/tvix/castore/src/fixtures.rs b/tvix/castore/src/fixtures.rs index 05bad916d55f..db0ee59daf60 100644 --- a/tvix/castore/src/fixtures.rs +++ b/tvix/castore/src/fixtures.rs @@ -1,104 +1,120 @@ +use bytes::Bytes; +use std::sync::LazyLock; + use crate::{B3Digest, Directory, Node}; -use lazy_static::lazy_static; pub const HELLOWORLD_BLOB_CONTENTS: &[u8] = b"Hello World!"; pub const EMPTY_BLOB_CONTENTS: &[u8] = b""; -lazy_static! { - pub static ref DUMMY_DIGEST: B3Digest = { - let u = [0u8; 32]; - (&u).into() - }; - pub static ref DUMMY_DIGEST_2: B3Digest = { - let mut u = [0u8; 32]; - u[0] = 0x10; - (&u).into() - }; - pub static ref DUMMY_DATA_1: bytes::Bytes = vec![0x01, 0x02, 0x03].into(); - pub static ref DUMMY_DATA_2: bytes::Bytes = vec![0x04, 0x05].into(); +pub static DUMMY_DIGEST: LazyLock<B3Digest> = LazyLock::new(|| (&[0u8; 32]).into()); +pub static DUMMY_DIGEST_2: LazyLock<B3Digest> = LazyLock::new(|| { + let mut u = [0u8; 32]; + u[0] = 0x10; + (&u).into() +}); +pub static DUMMY_DATA_1: LazyLock<Bytes> = LazyLock::new(|| vec![0x01, 0x02, 0x03].into()); +pub static DUMMY_DATA_2: LazyLock<Bytes> = LazyLock::new(|| vec![0x04, 0x05].into()); - pub static ref HELLOWORLD_BLOB_DIGEST: B3Digest = - blake3::hash(HELLOWORLD_BLOB_CONTENTS).as_bytes().into(); - pub static ref EMPTY_BLOB_DIGEST: B3Digest = - blake3::hash(EMPTY_BLOB_CONTENTS).as_bytes().into(); +pub static HELLOWORLD_BLOB_DIGEST: LazyLock<B3Digest> = + LazyLock::new(|| blake3::hash(HELLOWORLD_BLOB_CONTENTS).as_bytes().into()); +pub static EMPTY_BLOB_DIGEST: LazyLock<B3Digest> = + LazyLock::new(|| blake3::hash(EMPTY_BLOB_CONTENTS).as_bytes().into()); - // 2 bytes - pub static ref BLOB_A: bytes::Bytes = vec![0x00, 0x01].into(); - pub static ref BLOB_A_DIGEST: B3Digest = blake3::hash(&BLOB_A).as_bytes().into(); +// 2 bytes +pub static BLOB_A: LazyLock<Bytes> = LazyLock::new(|| vec![0x00, 0x01].into()); +pub static BLOB_A_DIGEST: LazyLock<B3Digest> = + LazyLock::new(|| blake3::hash(&BLOB_A).as_bytes().into()); - // 1MB - pub static ref BLOB_B: bytes::Bytes = (0..255).collect::<Vec<u8>>().repeat(4 * 1024).into(); - pub static ref BLOB_B_DIGEST: B3Digest = blake3::hash(&BLOB_B).as_bytes().into(); +// 1MB +pub static BLOB_B: LazyLock<Bytes> = + LazyLock::new(|| (0..255).collect::<Vec<u8>>().repeat(4 * 1024).into()); +pub static BLOB_B_DIGEST: LazyLock<B3Digest> = + LazyLock::new(|| blake3::hash(&BLOB_B).as_bytes().into()); - // Directories - pub static ref DIRECTORY_WITH_KEEP: Directory = Directory::try_from_iter([( +// Directories +pub static DIRECTORY_WITH_KEEP: LazyLock<Directory> = LazyLock::new(|| { + Directory::try_from_iter([( ".keep".try_into().unwrap(), - Node::File{ + Node::File { digest: EMPTY_BLOB_DIGEST.clone(), size: 0, - executable: false - })]).unwrap(); - pub static ref DIRECTORY_COMPLICATED: Directory = Directory::try_from_iter([ + executable: false, + }, + )]) + .unwrap() +}); +pub static DIRECTORY_COMPLICATED: LazyLock<Directory> = LazyLock::new(|| { + Directory::try_from_iter([ ( "keep".try_into().unwrap(), - Node::Directory{ + Node::Directory { digest: DIRECTORY_WITH_KEEP.digest(), - size: DIRECTORY_WITH_KEEP.size() - } + size: DIRECTORY_WITH_KEEP.size(), + }, ), ( ".keep".try_into().unwrap(), - Node::File{ + Node::File { digest: EMPTY_BLOB_DIGEST.clone(), size: 0, - executable: false - } + executable: false, + }, ), ( "aa".try_into().unwrap(), - Node::Symlink{ - target: "/nix/store/somewhereelse".try_into().unwrap() - } - ) - ]).unwrap(); - pub static ref DIRECTORY_A: Directory = Directory::new(); - pub static ref DIRECTORY_B: Directory = Directory::try_from_iter([( - "a".try_into().unwrap(), - Node::Directory{ - digest: DIRECTORY_A.digest(), - size: DIRECTORY_A.size(), - } - )]).unwrap(); - pub static ref DIRECTORY_C: Directory = Directory::try_from_iter([ + Node::Symlink { + target: "/nix/store/somewhereelse".try_into().unwrap(), + }, + ), + ]) + .unwrap() +}); +pub static DIRECTORY_A: LazyLock<Directory> = LazyLock::new(Directory::new); +pub static DIRECTORY_B: LazyLock<Directory> = LazyLock::new(|| { + Directory::try_from_iter([( + "a".try_into().unwrap(), + Node::Directory { + digest: DIRECTORY_A.digest(), + size: DIRECTORY_A.size(), + }, + )]) + .unwrap() +}); +pub static DIRECTORY_C: LazyLock<Directory> = LazyLock::new(|| { + Directory::try_from_iter([ ( "a".try_into().unwrap(), - Node::Directory{ + Node::Directory { digest: DIRECTORY_A.digest(), size: DIRECTORY_A.size(), - } + }, ), ( "a'".try_into().unwrap(), - Node::Directory{ + Node::Directory { digest: DIRECTORY_A.digest(), size: DIRECTORY_A.size(), - } - ) - ]).unwrap(); - pub static ref DIRECTORY_D: Directory = Directory::try_from_iter([ + }, + ), + ]) + .unwrap() +}); +pub static DIRECTORY_D: LazyLock<Directory> = LazyLock::new(|| { + Directory::try_from_iter([ ( "a".try_into().unwrap(), - Node::Directory{ + Node::Directory { digest: DIRECTORY_A.digest(), size: DIRECTORY_A.size(), - } + }, ), ( "b".try_into().unwrap(), - Node::Directory{ + Node::Directory { digest: DIRECTORY_B.digest(), size: DIRECTORY_B.size(), - } - ) - ]).unwrap(); -} + }, + ), + ]) + .unwrap() +}); |