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 <tazjin@tvl.su> Tested-by: BuildkiteCI Autosubmit: flokli <flokli@flokli.de>
This commit is contained in:
parent
fcfbcf9cfa
commit
ea48481eb3
1 changed files with 13 additions and 4 deletions
|
@ -8,6 +8,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
use std::sync::RwLock;
|
||||||
use std::{io, path::PathBuf};
|
use std::{io, path::PathBuf};
|
||||||
|
|
||||||
use smol_str::SmolStr;
|
use smol_str::SmolStr;
|
||||||
|
@ -24,7 +25,7 @@ pub struct NixCompatIO {
|
||||||
// TODO(tazjin): This could be done better by having a thunk cache
|
// 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
|
// for these calls on the eval side, but that is a little more
|
||||||
// complex.
|
// complex.
|
||||||
import_cache: HashMap<PathBuf, PathBuf>,
|
import_cache: RwLock<HashMap<PathBuf, PathBuf>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EvalIO for NixCompatIO {
|
impl EvalIO for NixCompatIO {
|
||||||
|
@ -35,13 +36,21 @@ impl EvalIO for NixCompatIO {
|
||||||
// Pass path imports through to `nix-store --add`
|
// Pass path imports through to `nix-store --add`
|
||||||
fn import_path(&mut self, path: &Path) -> Result<PathBuf, io::Error> {
|
fn import_path(&mut self, path: &Path) -> Result<PathBuf, io::Error> {
|
||||||
let path = path.to_owned();
|
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());
|
return Ok(path.to_path_buf());
|
||||||
}
|
}
|
||||||
|
|
||||||
let store_path = self.add_to_store(&path)?;
|
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)
|
Ok(store_path)
|
||||||
}
|
}
|
||||||
|
@ -78,7 +87,7 @@ impl NixCompatIO {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
NixCompatIO {
|
NixCompatIO {
|
||||||
underlying: StdIO,
|
underlying: StdIO,
|
||||||
import_cache: HashMap::new(),
|
import_cache: RwLock::new(HashMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue