about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2023-05-25T05·32+0300
committerclbot <clbot@tvl.fyi>2023-05-25T11·11+0000
commitea48481eb39ded96fbaeef5d8e25771197eedda2 (patch)
tree91fecdcdbfbb668ad26044b92fbf2ce62cc341bc
parentfcfbcf9cfa52e221056ea52f7b70e06ca0a36eba (diff)
refactor(tvix/cli): wrap NixCompatIO import_cache in RwLock r/6201
This allows dropping all &mut self from the EvalIO trait signature.

Change-Id: Ie127b0a459d2996636385d159fcc5f7147e74e2e
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8630
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
-rw-r--r--tvix/cli/src/nix_compat.rs17
1 files changed, 13 insertions, 4 deletions
diff --git a/tvix/cli/src/nix_compat.rs b/tvix/cli/src/nix_compat.rs
index b322886c9e..0da3aeb212 100644
--- a/tvix/cli/src/nix_compat.rs
+++ b/tvix/cli/src/nix_compat.rs
@@ -8,6 +8,7 @@
 use std::collections::HashMap;
 use std::path::Path;
 use std::process::Command;
+use std::sync::RwLock;
 use std::{io, path::PathBuf};
 
 use smol_str::SmolStr;
@@ -24,7 +25,7 @@ pub struct NixCompatIO {
     // TODO(tazjin): This could be done better by having a thunk cache
     // for these calls on the eval side, but that is a little more
     // complex.
-    import_cache: HashMap<PathBuf, PathBuf>,
+    import_cache: RwLock<HashMap<PathBuf, PathBuf>>,
 }
 
 impl EvalIO for NixCompatIO {
@@ -35,13 +36,21 @@ impl EvalIO for NixCompatIO {
     // Pass path imports through to `nix-store --add`
     fn import_path(&mut self, path: &Path) -> Result<PathBuf, io::Error> {
         let path = path.to_owned();
-        if let Some(path) = self.import_cache.get(&path) {
+        if let Some(path) = self
+            .import_cache
+            .read()
+            .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?
+            .get(&path)
+        {
             return Ok(path.to_path_buf());
         }
 
         let store_path = self.add_to_store(&path)?;
 
-        self.import_cache.insert(path, store_path.clone());
+        self.import_cache
+            .write()
+            .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?
+            .insert(path, store_path.clone());
 
         Ok(store_path)
     }
@@ -78,7 +87,7 @@ impl NixCompatIO {
     pub fn new() -> Self {
         NixCompatIO {
             underlying: StdIO,
-            import_cache: HashMap::new(),
+            import_cache: RwLock::new(HashMap::new()),
         }
     }