From ea48481eb39ded96fbaeef5d8e25771197eedda2 Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Thu, 25 May 2023 08:32:13 +0300 Subject: refactor(tvix/cli): wrap NixCompatIO import_cache in RwLock 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 Tested-by: BuildkiteCI Autosubmit: flokli --- tvix/cli/src/nix_compat.rs | 17 +++++++++++++---- 1 file 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, + import_cache: RwLock>, } 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 { 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()), } } -- cgit 1.4.1