refactor(tvix/castore): remove use of lazy_static
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>
This commit is contained in:
parent
a94414e7ff
commit
cdbdd2d04e
5 changed files with 145 additions and 126 deletions
|
@ -253,14 +253,16 @@ where
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::{io::SeekFrom, sync::Arc};
|
use std::{
|
||||||
|
io::SeekFrom,
|
||||||
|
sync::{Arc, LazyLock},
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
blobservice::{chunked_reader::ChunkedReader, BlobService, MemoryBlobService},
|
blobservice::{chunked_reader::ChunkedReader, BlobService, MemoryBlobService},
|
||||||
B3Digest,
|
B3Digest,
|
||||||
};
|
};
|
||||||
use hex_literal::hex;
|
use hex_literal::hex;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
use tokio::io::{AsyncReadExt, AsyncSeekExt};
|
||||||
|
|
||||||
const CHUNK_1: [u8; 2] = hex!("0001");
|
const CHUNK_1: [u8; 2] = hex!("0001");
|
||||||
|
@ -269,21 +271,26 @@ mod test {
|
||||||
const CHUNK_4: [u8; 2] = hex!("0708");
|
const CHUNK_4: [u8; 2] = hex!("0708");
|
||||||
const CHUNK_5: [u8; 7] = hex!("090a0b0c0d0e0f");
|
const CHUNK_5: [u8; 7] = hex!("090a0b0c0d0e0f");
|
||||||
|
|
||||||
lazy_static! {
|
// `[ 0 1 ] [ 2 3 4 5 ] [ 6 ] [ 7 8 ] [ 9 10 11 12 13 14 15 ]`
|
||||||
// `[ 0 1 ] [ 2 3 4 5 ] [ 6 ] [ 7 8 ] [ 9 10 11 12 13 14 15 ]`
|
pub static CHUNK_1_DIGEST: LazyLock<B3Digest> =
|
||||||
pub static ref CHUNK_1_DIGEST: B3Digest = blake3::hash(&CHUNK_1).as_bytes().into();
|
LazyLock::new(|| blake3::hash(&CHUNK_1).as_bytes().into());
|
||||||
pub static ref CHUNK_2_DIGEST: B3Digest = blake3::hash(&CHUNK_2).as_bytes().into();
|
pub static CHUNK_2_DIGEST: LazyLock<B3Digest> =
|
||||||
pub static ref CHUNK_3_DIGEST: B3Digest = blake3::hash(&CHUNK_3).as_bytes().into();
|
LazyLock::new(|| blake3::hash(&CHUNK_2).as_bytes().into());
|
||||||
pub static ref CHUNK_4_DIGEST: B3Digest = blake3::hash(&CHUNK_4).as_bytes().into();
|
pub static CHUNK_3_DIGEST: LazyLock<B3Digest> =
|
||||||
pub static ref CHUNK_5_DIGEST: B3Digest = blake3::hash(&CHUNK_5).as_bytes().into();
|
LazyLock::new(|| blake3::hash(&CHUNK_3).as_bytes().into());
|
||||||
pub static ref BLOB_1_LIST: [(B3Digest, u64); 5] = [
|
pub static CHUNK_4_DIGEST: LazyLock<B3Digest> =
|
||||||
|
LazyLock::new(|| blake3::hash(&CHUNK_4).as_bytes().into());
|
||||||
|
pub static CHUNK_5_DIGEST: LazyLock<B3Digest> =
|
||||||
|
LazyLock::new(|| blake3::hash(&CHUNK_5).as_bytes().into());
|
||||||
|
pub static BLOB_1_LIST: LazyLock<[(B3Digest, u64); 5]> = LazyLock::new(|| {
|
||||||
|
[
|
||||||
(CHUNK_1_DIGEST.clone(), 2),
|
(CHUNK_1_DIGEST.clone(), 2),
|
||||||
(CHUNK_2_DIGEST.clone(), 4),
|
(CHUNK_2_DIGEST.clone(), 4),
|
||||||
(CHUNK_3_DIGEST.clone(), 1),
|
(CHUNK_3_DIGEST.clone(), 1),
|
||||||
(CHUNK_4_DIGEST.clone(), 2),
|
(CHUNK_4_DIGEST.clone(), 2),
|
||||||
(CHUNK_5_DIGEST.clone(), 7),
|
(CHUNK_5_DIGEST.clone(), 7),
|
||||||
];
|
]
|
||||||
}
|
});
|
||||||
|
|
||||||
use super::ChunkedBlob;
|
use super::ChunkedBlob;
|
||||||
|
|
||||||
|
|
|
@ -276,23 +276,21 @@ impl ValidatedDirectoryGraph {
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::fixtures::{DIRECTORY_A, DIRECTORY_B, DIRECTORY_C};
|
use crate::fixtures::{DIRECTORY_A, DIRECTORY_B, DIRECTORY_C};
|
||||||
use crate::{Directory, Node};
|
use crate::{Directory, Node};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use super::{DirectoryGraph, LeavesToRootValidator, RootToLeavesValidator};
|
use super::{DirectoryGraph, LeavesToRootValidator, RootToLeavesValidator};
|
||||||
|
|
||||||
lazy_static! {
|
pub static BROKEN_PARENT_DIRECTORY: LazyLock<Directory> = LazyLock::new(|| {
|
||||||
pub static ref BROKEN_PARENT_DIRECTORY: Directory =
|
Directory::try_from_iter([(
|
||||||
Directory::try_from_iter([
|
"foo".try_into().unwrap(),
|
||||||
(
|
Node::Directory {
|
||||||
"foo".try_into().unwrap(),
|
digest: DIRECTORY_A.digest(),
|
||||||
Node::Directory{
|
size: DIRECTORY_A.size() + 42, // wrong!
|
||||||
digest: DIRECTORY_A.digest(),
|
},
|
||||||
size: DIRECTORY_A.size() + 42, // wrong!
|
)])
|
||||||
}
|
.unwrap()
|
||||||
)
|
});
|
||||||
]).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
/// Uploading an empty directory should succeed.
|
/// Uploading an empty directory should succeed.
|
||||||
|
|
|
@ -45,15 +45,14 @@ pub async fn from_addr(
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use super::from_addr;
|
use super::from_addr;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
use tempfile::TempDir;
|
use tempfile::TempDir;
|
||||||
|
|
||||||
lazy_static! {
|
static TMPDIR_REDB_1: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
|
||||||
static ref TMPDIR_REDB_1: TempDir = TempDir::new().unwrap();
|
static TMPDIR_REDB_2: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
|
||||||
static ref TMPDIR_REDB_2: TempDir = TempDir::new().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
/// This uses an unsupported scheme.
|
/// This uses an unsupported scheme.
|
||||||
|
|
|
@ -1,104 +1,120 @@
|
||||||
|
use bytes::Bytes;
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use crate::{B3Digest, Directory, Node};
|
use crate::{B3Digest, Directory, Node};
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
pub const HELLOWORLD_BLOB_CONTENTS: &[u8] = b"Hello World!";
|
pub const HELLOWORLD_BLOB_CONTENTS: &[u8] = b"Hello World!";
|
||||||
pub const EMPTY_BLOB_CONTENTS: &[u8] = b"";
|
pub const EMPTY_BLOB_CONTENTS: &[u8] = b"";
|
||||||
|
|
||||||
lazy_static! {
|
pub static DUMMY_DIGEST: LazyLock<B3Digest> = LazyLock::new(|| (&[0u8; 32]).into());
|
||||||
pub static ref DUMMY_DIGEST: B3Digest = {
|
pub static DUMMY_DIGEST_2: LazyLock<B3Digest> = LazyLock::new(|| {
|
||||||
let u = [0u8; 32];
|
let mut u = [0u8; 32];
|
||||||
(&u).into()
|
u[0] = 0x10;
|
||||||
};
|
(&u).into()
|
||||||
pub static ref DUMMY_DIGEST_2: B3Digest = {
|
});
|
||||||
let mut u = [0u8; 32];
|
pub static DUMMY_DATA_1: LazyLock<Bytes> = LazyLock::new(|| vec![0x01, 0x02, 0x03].into());
|
||||||
u[0] = 0x10;
|
pub static DUMMY_DATA_2: LazyLock<Bytes> = LazyLock::new(|| vec![0x04, 0x05].into());
|
||||||
(&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 ref HELLOWORLD_BLOB_DIGEST: B3Digest =
|
pub static HELLOWORLD_BLOB_DIGEST: LazyLock<B3Digest> =
|
||||||
blake3::hash(HELLOWORLD_BLOB_CONTENTS).as_bytes().into();
|
LazyLock::new(|| blake3::hash(HELLOWORLD_BLOB_CONTENTS).as_bytes().into());
|
||||||
pub static ref EMPTY_BLOB_DIGEST: B3Digest =
|
pub static EMPTY_BLOB_DIGEST: LazyLock<B3Digest> =
|
||||||
blake3::hash(EMPTY_BLOB_CONTENTS).as_bytes().into();
|
LazyLock::new(|| blake3::hash(EMPTY_BLOB_CONTENTS).as_bytes().into());
|
||||||
|
|
||||||
// 2 bytes
|
// 2 bytes
|
||||||
pub static ref BLOB_A: bytes::Bytes = vec![0x00, 0x01].into();
|
pub static BLOB_A: LazyLock<Bytes> = LazyLock::new(|| vec![0x00, 0x01].into());
|
||||||
pub static ref BLOB_A_DIGEST: B3Digest = blake3::hash(&BLOB_A).as_bytes().into();
|
pub static BLOB_A_DIGEST: LazyLock<B3Digest> =
|
||||||
|
LazyLock::new(|| blake3::hash(&BLOB_A).as_bytes().into());
|
||||||
|
|
||||||
// 1MB
|
// 1MB
|
||||||
pub static ref BLOB_B: bytes::Bytes = (0..255).collect::<Vec<u8>>().repeat(4 * 1024).into();
|
pub static BLOB_B: LazyLock<Bytes> =
|
||||||
pub static ref BLOB_B_DIGEST: B3Digest = blake3::hash(&BLOB_B).as_bytes().into();
|
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
|
// Directories
|
||||||
pub static ref DIRECTORY_WITH_KEEP: Directory = Directory::try_from_iter([(
|
pub static DIRECTORY_WITH_KEEP: LazyLock<Directory> = LazyLock::new(|| {
|
||||||
|
Directory::try_from_iter([(
|
||||||
".keep".try_into().unwrap(),
|
".keep".try_into().unwrap(),
|
||||||
Node::File{
|
Node::File {
|
||||||
digest: EMPTY_BLOB_DIGEST.clone(),
|
digest: EMPTY_BLOB_DIGEST.clone(),
|
||||||
size: 0,
|
size: 0,
|
||||||
executable: false
|
executable: false,
|
||||||
})]).unwrap();
|
},
|
||||||
pub static ref DIRECTORY_COMPLICATED: Directory = Directory::try_from_iter([
|
)])
|
||||||
|
.unwrap()
|
||||||
|
});
|
||||||
|
pub static DIRECTORY_COMPLICATED: LazyLock<Directory> = LazyLock::new(|| {
|
||||||
|
Directory::try_from_iter([
|
||||||
(
|
(
|
||||||
"keep".try_into().unwrap(),
|
"keep".try_into().unwrap(),
|
||||||
Node::Directory{
|
Node::Directory {
|
||||||
digest: DIRECTORY_WITH_KEEP.digest(),
|
digest: DIRECTORY_WITH_KEEP.digest(),
|
||||||
size: DIRECTORY_WITH_KEEP.size()
|
size: DIRECTORY_WITH_KEEP.size(),
|
||||||
}
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
".keep".try_into().unwrap(),
|
".keep".try_into().unwrap(),
|
||||||
Node::File{
|
Node::File {
|
||||||
digest: EMPTY_BLOB_DIGEST.clone(),
|
digest: EMPTY_BLOB_DIGEST.clone(),
|
||||||
size: 0,
|
size: 0,
|
||||||
executable: false
|
executable: false,
|
||||||
}
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"aa".try_into().unwrap(),
|
"aa".try_into().unwrap(),
|
||||||
Node::Symlink{
|
Node::Symlink {
|
||||||
target: "/nix/store/somewhereelse".try_into().unwrap()
|
target: "/nix/store/somewhereelse".try_into().unwrap(),
|
||||||
}
|
},
|
||||||
)
|
),
|
||||||
]).unwrap();
|
])
|
||||||
pub static ref DIRECTORY_A: Directory = Directory::new();
|
.unwrap()
|
||||||
pub static ref DIRECTORY_B: Directory = Directory::try_from_iter([(
|
});
|
||||||
"a".try_into().unwrap(),
|
pub static DIRECTORY_A: LazyLock<Directory> = LazyLock::new(Directory::new);
|
||||||
Node::Directory{
|
pub static DIRECTORY_B: LazyLock<Directory> = LazyLock::new(|| {
|
||||||
digest: DIRECTORY_A.digest(),
|
Directory::try_from_iter([(
|
||||||
size: DIRECTORY_A.size(),
|
"a".try_into().unwrap(),
|
||||||
}
|
Node::Directory {
|
||||||
)]).unwrap();
|
digest: DIRECTORY_A.digest(),
|
||||||
pub static ref DIRECTORY_C: Directory = Directory::try_from_iter([
|
size: DIRECTORY_A.size(),
|
||||||
|
},
|
||||||
|
)])
|
||||||
|
.unwrap()
|
||||||
|
});
|
||||||
|
pub static DIRECTORY_C: LazyLock<Directory> = LazyLock::new(|| {
|
||||||
|
Directory::try_from_iter([
|
||||||
(
|
(
|
||||||
"a".try_into().unwrap(),
|
"a".try_into().unwrap(),
|
||||||
Node::Directory{
|
Node::Directory {
|
||||||
digest: DIRECTORY_A.digest(),
|
digest: DIRECTORY_A.digest(),
|
||||||
size: DIRECTORY_A.size(),
|
size: DIRECTORY_A.size(),
|
||||||
}
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"a'".try_into().unwrap(),
|
"a'".try_into().unwrap(),
|
||||||
Node::Directory{
|
Node::Directory {
|
||||||
digest: DIRECTORY_A.digest(),
|
digest: DIRECTORY_A.digest(),
|
||||||
size: DIRECTORY_A.size(),
|
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(),
|
"a".try_into().unwrap(),
|
||||||
Node::Directory{
|
Node::Directory {
|
||||||
digest: DIRECTORY_A.digest(),
|
digest: DIRECTORY_A.digest(),
|
||||||
size: DIRECTORY_A.size(),
|
size: DIRECTORY_A.size(),
|
||||||
}
|
},
|
||||||
),
|
),
|
||||||
(
|
(
|
||||||
"b".try_into().unwrap(),
|
"b".try_into().unwrap(),
|
||||||
Node::Directory{
|
Node::Directory {
|
||||||
digest: DIRECTORY_B.digest(),
|
digest: DIRECTORY_B.digest(),
|
||||||
size: DIRECTORY_B.size(),
|
size: DIRECTORY_B.size(),
|
||||||
}
|
},
|
||||||
)
|
),
|
||||||
]).unwrap();
|
])
|
||||||
}
|
.unwrap()
|
||||||
|
});
|
||||||
|
|
|
@ -292,44 +292,43 @@ impl IngestionEntryGraph {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
|
use super::{Error, IngestionEntryGraph};
|
||||||
use crate::import::IngestionEntry;
|
use crate::import::IngestionEntry;
|
||||||
use crate::B3Digest;
|
use crate::B3Digest;
|
||||||
|
|
||||||
use super::{Error, IngestionEntryGraph};
|
|
||||||
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use rstest::rstest;
|
use rstest::rstest;
|
||||||
|
|
||||||
lazy_static! {
|
pub static EMPTY_DIGEST: LazyLock<B3Digest> =
|
||||||
pub static ref EMPTY_DIGEST: B3Digest = blake3::hash(&[]).as_bytes().into();
|
LazyLock::new(|| blake3::hash(&[]).as_bytes().into());
|
||||||
pub static ref DIR_A: IngestionEntry = IngestionEntry::Dir {
|
pub static DIR_A: LazyLock<IngestionEntry> = LazyLock::new(|| IngestionEntry::Dir {
|
||||||
path: "a".parse().unwrap()
|
path: "a".parse().unwrap(),
|
||||||
};
|
});
|
||||||
pub static ref DIR_B: IngestionEntry = IngestionEntry::Dir {
|
pub static DIR_B: LazyLock<IngestionEntry> = LazyLock::new(|| IngestionEntry::Dir {
|
||||||
path: "b".parse().unwrap()
|
path: "b".parse().unwrap(),
|
||||||
};
|
});
|
||||||
pub static ref DIR_A_B: IngestionEntry = IngestionEntry::Dir {
|
pub static DIR_A_B: LazyLock<IngestionEntry> = LazyLock::new(|| IngestionEntry::Dir {
|
||||||
path: "a/b".parse().unwrap()
|
path: "a/b".parse().unwrap(),
|
||||||
};
|
});
|
||||||
pub static ref FILE_A: IngestionEntry = IngestionEntry::Regular {
|
pub static FILE_A: LazyLock<IngestionEntry> = LazyLock::new(|| IngestionEntry::Regular {
|
||||||
path: "a".parse().unwrap(),
|
path: "a".parse().unwrap(),
|
||||||
size: 0,
|
size: 0,
|
||||||
executable: false,
|
executable: false,
|
||||||
digest: EMPTY_DIGEST.clone(),
|
digest: EMPTY_DIGEST.clone(),
|
||||||
};
|
});
|
||||||
pub static ref FILE_A_B: IngestionEntry = IngestionEntry::Regular {
|
pub static FILE_A_B: LazyLock<IngestionEntry> = LazyLock::new(|| IngestionEntry::Regular {
|
||||||
path: "a/b".parse().unwrap(),
|
path: "a/b".parse().unwrap(),
|
||||||
size: 0,
|
size: 0,
|
||||||
executable: false,
|
executable: false,
|
||||||
digest: EMPTY_DIGEST.clone(),
|
digest: EMPTY_DIGEST.clone(),
|
||||||
};
|
});
|
||||||
pub static ref FILE_A_B_C: IngestionEntry = IngestionEntry::Regular {
|
pub static FILE_A_B_C: LazyLock<IngestionEntry> = LazyLock::new(|| IngestionEntry::Regular {
|
||||||
path: "a/b/c".parse().unwrap(),
|
path: "a/b/c".parse().unwrap(),
|
||||||
size: 0,
|
size: 0,
|
||||||
executable: false,
|
executable: false,
|
||||||
digest: EMPTY_DIGEST.clone(),
|
digest: EMPTY_DIGEST.clone(),
|
||||||
};
|
});
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[case::implicit_directories(&[&*FILE_A_B_C], &[&*FILE_A_B_C, &*DIR_A_B, &*DIR_A])]
|
#[case::implicit_directories(&[&*FILE_A_B_C], &[&*FILE_A_B_C, &*DIR_A_B, &*DIR_A])]
|
||||||
|
|
Loading…
Reference in a new issue