about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2024-10-13T10·30+0300
committerclbot <clbot@tvl.fyi>2024-10-13T14·45+0000
commit4beee4cba79efda4145e84c12ecbc876f39f2134 (patch)
tree680ce25d0c475f8fc13c2290dc5db6ab053b225a /tvix
parent5faf7c9d7b5fcf8fb2795b5879aece45d7c62b21 (diff)
refactor(tvix/glue): remove use of lazy_static r/8803
This is now supported in the standard library via std::sync::LazyLock, but
requires some manual shuffling around of code.

Change-Id: Ibb3be8458b8a8912ea04c9360d64c5cf914254d4
Reviewed-on: https://cl.tvl.fyi/c/depot/+/12609
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix')
-rw-r--r--tvix/Cargo.lock1
-rw-r--r--tvix/Cargo.nix4
-rw-r--r--tvix/glue/Cargo.toml1
-rw-r--r--tvix/glue/benches/eval.rs7
-rw-r--r--tvix/glue/src/known_paths.rs83
-rw-r--r--tvix/glue/src/tvix_build.rs18
6 files changed, 64 insertions, 50 deletions
diff --git a/tvix/Cargo.lock b/tvix/Cargo.lock
index d6a0aee59495..d57bc04314c4 100644
--- a/tvix/Cargo.lock
+++ b/tvix/Cargo.lock
@@ -4721,7 +4721,6 @@ dependencies = [
  "data-encoding",
  "futures",
  "hex-literal",
- "lazy_static",
  "magic",
  "md-5",
  "mimalloc",
diff --git a/tvix/Cargo.nix b/tvix/Cargo.nix
index d2cb0c4c514f..ed955fa56dd6 100644
--- a/tvix/Cargo.nix
+++ b/tvix/Cargo.nix
@@ -15848,10 +15848,6 @@ rec {
             packageId = "hex-literal";
           }
           {
-            name = "lazy_static";
-            packageId = "lazy_static";
-          }
-          {
             name = "mimalloc";
             packageId = "mimalloc";
           }
diff --git a/tvix/glue/Cargo.toml b/tvix/glue/Cargo.toml
index 4836e1e3a5d8..5fac947125af 100644
--- a/tvix/glue/Cargo.toml
+++ b/tvix/glue/Cargo.toml
@@ -36,7 +36,6 @@ clap = { workspace = true }
 [dev-dependencies]
 criterion = { workspace = true, features = ["html_reports"] }
 hex-literal = { workspace = true }
-lazy_static = { workspace = true }
 mimalloc = { workspace = true }
 nix = { workspace = true, features = ["fs"] }
 pretty_assertions = { workspace = true }
diff --git a/tvix/glue/benches/eval.rs b/tvix/glue/benches/eval.rs
index a0823d2e129b..ce14bdcc13ec 100644
--- a/tvix/glue/benches/eval.rs
+++ b/tvix/glue/benches/eval.rs
@@ -1,7 +1,7 @@
 use clap::Parser;
 use criterion::{black_box, criterion_group, criterion_main, Criterion};
-use lazy_static::lazy_static;
 use mimalloc::MiMalloc;
+use std::sync::LazyLock;
 use std::{env, rc::Rc, sync::Arc, time::Duration};
 use tvix_build::buildservice::DummyBuildService;
 use tvix_eval::{builtins::impure_builtins, EvalIO};
@@ -16,9 +16,8 @@ use tvix_store::utils::{construct_services, ServiceUrlsMemory};
 #[global_allocator]
 static GLOBAL: MiMalloc = MiMalloc;
 
-lazy_static! {
-    static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Runtime::new().unwrap();
-}
+static TOKIO_RUNTIME: LazyLock<tokio::runtime::Runtime> =
+    LazyLock::new(|| tokio::runtime::Runtime::new().unwrap());
 
 fn interpret(code: &str) {
     // TODO: this is a bit annoying.
diff --git a/tvix/glue/src/known_paths.rs b/tvix/glue/src/known_paths.rs
index 239acca9829a..7934bfe0baa2 100644
--- a/tvix/glue/src/known_paths.rs
+++ b/tvix/glue/src/known_paths.rs
@@ -144,48 +144,69 @@ impl KnownPaths {
 
 #[cfg(test)]
 mod tests {
+    use std::sync::LazyLock;
+
+    use hex_literal::hex;
     use nix_compat::{derivation::Derivation, nixbase32, nixhash, store_path::StorePath};
     use url::Url;
 
-    use crate::fetchers::Fetch;
-
     use super::KnownPaths;
-    use hex_literal::hex;
-    use lazy_static::lazy_static;
+    use crate::fetchers::Fetch;
 
-    lazy_static! {
-        static ref BAR_DRV: Derivation = Derivation::from_aterm_bytes(include_bytes!(
+    static BAR_DRV: LazyLock<Derivation> = LazyLock::new(|| {
+        Derivation::from_aterm_bytes(include_bytes!(
             "tests/ss2p4wmxijn652haqyd7dckxwl4c7hxx-bar.drv"
         ))
-        .expect("must parse");
-        static ref FOO_DRV: Derivation = Derivation::from_aterm_bytes(include_bytes!(
+        .expect("must parse")
+    });
+
+    static FOO_DRV: LazyLock<Derivation> = LazyLock::new(|| {
+        Derivation::from_aterm_bytes(include_bytes!(
             "tests/ch49594n9avinrf8ip0aslidkc4lxkqv-foo.drv"
         ))
-        .expect("must parse");
-        static ref BAR_DRV_PATH: StorePath<String> =
-            StorePath::from_bytes(b"ss2p4wmxijn652haqyd7dckxwl4c7hxx-bar.drv").expect("must parse");
-        static ref FOO_DRV_PATH: StorePath<String> =
-            StorePath::from_bytes(b"ch49594n9avinrf8ip0aslidkc4lxkqv-foo.drv").expect("must parse");
-        static ref BAR_OUT_PATH: StorePath<String> =
-            StorePath::from_bytes(b"mp57d33657rf34lzvlbpfa1gjfv5gmpg-bar").expect("must parse");
-        static ref FOO_OUT_PATH: StorePath<String> =
-            StorePath::from_bytes(b"fhaj6gmwns62s6ypkcldbaj2ybvkhx3p-foo").expect("must parse");
-
-        static ref FETCH_URL : Fetch = Fetch::URL{
-            url: Url::parse("https://raw.githubusercontent.com/aaptel/notmuch-extract-patch/f732a53e12a7c91a06755ebfab2007adc9b3063b/notmuch-extract-patch").unwrap(),
-            exp_hash: Some(nixhash::from_sri_str("sha256-Xa1Jbl2Eq5+L0ww+Ph1osA3Z/Dxe/RkN1/dITQCdXFk=").unwrap())
-        };
-        static ref FETCH_URL_OUT_PATH: StorePath<String> = StorePath::from_bytes(b"06qi00hylriyfm0nl827crgjvbax84mz-notmuch-extract-patch").unwrap();
-
-        static ref FETCH_TARBALL : Fetch = Fetch::Tarball{
-            url: Url::parse("https://github.com/NixOS/nixpkgs/archive/91050ea1e57e50388fa87a3302ba12d188ef723a.tar.gz").unwrap(),
-            exp_nar_sha256: Some(nixbase32::decode_fixed("1hf6cgaci1n186kkkjq106ryf8mmlq9vnwgfwh625wa8hfgdn4dm").unwrap())
-        };
-        static ref FETCH_TARBALL_OUT_PATH: StorePath<String> = StorePath::from_bytes(b"7adgvk5zdfq4pwrhsm3n9lzypb12gw0g-source").unwrap();
+        .expect("must parse")
+    });
+
+    static BAR_DRV_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
+        StorePath::from_bytes(b"ss2p4wmxijn652haqyd7dckxwl4c7hxx-bar.drv").expect("must parse")
+    });
+
+    static FOO_DRV_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
+        StorePath::from_bytes(b"ch49594n9avinrf8ip0aslidkc4lxkqv-foo.drv").expect("must parse")
+    });
+
+    static BAR_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
+        StorePath::from_bytes(b"mp57d33657rf34lzvlbpfa1gjfv5gmpg-bar").expect("must parse")
+    });
+
+    static FOO_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
+        StorePath::from_bytes(b"fhaj6gmwns62s6ypkcldbaj2ybvkhx3p-foo").expect("must parse")
+    });
+
+    static FETCH_URL: LazyLock<Fetch> = LazyLock::new(|| {
+        Fetch::URL {
+        url: Url::parse("https://raw.githubusercontent.com/aaptel/notmuch-extract-patch/f732a53e12a7c91a06755ebfab2007adc9b3063b/notmuch-extract-patch").unwrap(),
+        exp_hash: Some(nixhash::from_sri_str("sha256-Xa1Jbl2Eq5+L0ww+Ph1osA3Z/Dxe/RkN1/dITQCdXFk=").unwrap())
     }
+    });
+
+    static FETCH_URL_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
+        StorePath::from_bytes(b"06qi00hylriyfm0nl827crgjvbax84mz-notmuch-extract-patch").unwrap()
+    });
+
+    static FETCH_TARBALL: LazyLock<Fetch> = LazyLock::new(|| {
+        Fetch::Tarball {
+        url: Url::parse("https://github.com/NixOS/nixpkgs/archive/91050ea1e57e50388fa87a3302ba12d188ef723a.tar.gz").unwrap(),
+        exp_nar_sha256: Some(nixbase32::decode_fixed("1hf6cgaci1n186kkkjq106ryf8mmlq9vnwgfwh625wa8hfgdn4dm").unwrap())
+    }
+    });
+
+    static FETCH_TARBALL_OUT_PATH: LazyLock<StorePath<String>> = LazyLock::new(|| {
+        StorePath::from_bytes(b"7adgvk5zdfq4pwrhsm3n9lzypb12gw0g-source").unwrap()
+    });
 
-    /// ensure we don't allow acdding a Derivation that depends on another,
-    /// not-yet-added Derivation.
+    /// Ensure that we don't allow adding a derivation that depends on another,
+    /// not-yet-added derivation.
     #[test]
     #[should_panic]
     fn drv_reject_if_missing_input_drv() {
diff --git a/tvix/glue/src/tvix_build.rs b/tvix/glue/src/tvix_build.rs
index 7a901a593047..e832dcf4b1c6 100644
--- a/tvix/glue/src/tvix_build.rs
+++ b/tvix/glue/src/tvix_build.rs
@@ -213,6 +213,7 @@ mod test {
     use bytes::Bytes;
     use nix_compat::derivation::Derivation;
     use std::collections::BTreeMap;
+    use std::sync::LazyLock;
     use tvix_build::proto::{
         build_request::{AdditionalFile, BuildConstraints, EnvVar},
         BuildRequest,
@@ -223,15 +224,14 @@ mod test {
     use crate::tvix_build::NIX_ENVIRONMENT_VARS;
 
     use super::derivation_to_build_request;
-    use lazy_static::lazy_static;
-
-    lazy_static! {
-        static ref INPUT_NODE_FOO_NAME: Bytes = "mp57d33657rf34lzvlbpfa1gjfv5gmpg-bar".into();
-        static ref INPUT_NODE_FOO: Node = Node::Directory {
-            digest: DUMMY_DIGEST.clone(),
-            size: 42
-        };
-    }
+
+    static INPUT_NODE_FOO_NAME: LazyLock<Bytes> =
+        LazyLock::new(|| "mp57d33657rf34lzvlbpfa1gjfv5gmpg-bar".into());
+
+    static INPUT_NODE_FOO: LazyLock<Node> = LazyLock::new(|| Node::Directory {
+        digest: DUMMY_DIGEST.clone(),
+        size: 42,
+    });
 
     #[test]
     fn test_derivation_to_build_request() {