about summary refs log tree commit diff
path: root/tvix/eval/src/nix_search_path.rs
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-01-16T12·19+0200
committerflokli <flokli@flokli.de>2024-01-18T09·09+0000
commite0a867cabff021348cc283b25467cfd40b8eb15a (patch)
tree9b4f1fd63460ba8385b38259481a7bb32363801b /tvix/eval/src/nix_search_path.rs
parent44d24852c3c62320cb2a4c9b9627e744c518f207 (diff)
refactor(tvix/eval): generalize EvalIO container r/7407
Don't restrict to a Box<dyn EvalIO>.

There's still one or two places where we do restrict, this will be
solved by b/262.

Change-Id: Ic8d927d6ea81fa12d90b1e4352f35ffaafbd1adf
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10639
Tested-by: BuildkiteCI
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
Diffstat (limited to 'tvix/eval/src/nix_search_path.rs')
-rw-r--r--tvix/eval/src/nix_search_path.rs38
1 files changed, 19 insertions, 19 deletions
diff --git a/tvix/eval/src/nix_search_path.rs b/tvix/eval/src/nix_search_path.rs
index f86cffd6d615..2f6d444475ff 100644
--- a/tvix/eval/src/nix_search_path.rs
+++ b/tvix/eval/src/nix_search_path.rs
@@ -68,11 +68,10 @@ impl NixSearchPathEntry {
     ///
     /// For prefixed path, an entry matches if the prefix does.
     // TODO(tazjin): verify these rules in the C++ impl, seems fishy.
-    fn resolve(
-        &self,
-        io: &mut dyn EvalIO,
-        lookup_path: &Path,
-    ) -> Result<Option<PathBuf>, ErrorKind> {
+    fn resolve<IO>(&self, io: IO, lookup_path: &Path) -> Result<Option<PathBuf>, ErrorKind>
+    where
+        IO: AsRef<dyn EvalIO>,
+    {
         let path = match self {
             NixSearchPathEntry::Path(parent) => canonicalise(parent.join(lookup_path))?,
 
@@ -85,7 +84,7 @@ impl NixSearchPathEntry {
             }
         };
 
-        if io.path_exists(&path).map_err(|e| ErrorKind::IO {
+        if io.as_ref().path_exists(&path).map_err(|e| ErrorKind::IO {
             path: Some(path.clone()),
             error: e.into(),
         })? {
@@ -124,17 +123,18 @@ pub struct NixSearchPath {
 impl NixSearchPath {
     /// Attempt to resolve the given `path` within this [`NixSearchPath`] using the
     /// path resolution rules for `<...>`-style paths
-    pub fn resolve<P>(
+    pub fn resolve<P, IO>(
         &self,
-        io: &mut dyn EvalIO,
+        io: IO,
         path: P,
     ) -> Result<Result<PathBuf, CatchableErrorKind>, ErrorKind>
     where
         P: AsRef<Path>,
+        IO: AsRef<dyn EvalIO>,
     {
         let path = path.as_ref();
         for entry in &self.entries {
-            if let Some(p) = entry.resolve(io, path)? {
+            if let Some(p) = entry.resolve(&io, path)? {
                 return Ok(Ok(p));
             }
         }
@@ -204,8 +204,8 @@ mod tests {
         #[test]
         fn simple_dir() {
             let nix_search_path = NixSearchPath::from_str("./.").unwrap();
-            let mut io = StdIO {};
-            let res = nix_search_path.resolve(&mut io, "src").unwrap();
+            let io = Box::new(StdIO {}) as Box<dyn EvalIO>;
+            let res = nix_search_path.resolve(&io, "src").unwrap();
             assert_eq!(
                 res.unwrap().to_path_buf(),
                 current_dir().unwrap().join("src").clean()
@@ -215,8 +215,8 @@ mod tests {
         #[test]
         fn failed_resolution() {
             let nix_search_path = NixSearchPath::from_str("./.").unwrap();
-            let mut io = StdIO {};
-            let err = nix_search_path.resolve(&mut io, "nope").unwrap();
+            let io = Box::new(StdIO {}) as Box<dyn EvalIO>;
+            let err = nix_search_path.resolve(&io, "nope").unwrap();
             assert!(
                 matches!(err, Err(CatchableErrorKind::NixPathResolution(..))),
                 "err = {err:?}"
@@ -226,16 +226,16 @@ mod tests {
         #[test]
         fn second_in_path() {
             let nix_search_path = NixSearchPath::from_str("./.:/").unwrap();
-            let mut io = StdIO {};
-            let res = nix_search_path.resolve(&mut io, "etc").unwrap();
+            let io = Box::new(StdIO {}) as Box<dyn EvalIO>;
+            let res = nix_search_path.resolve(&io, "etc").unwrap();
             assert_eq!(res.unwrap().to_path_buf(), Path::new("/etc"));
         }
 
         #[test]
         fn prefix() {
             let nix_search_path = NixSearchPath::from_str("/:tvix=.").unwrap();
-            let mut io = StdIO {};
-            let res = nix_search_path.resolve(&mut io, "tvix/src").unwrap();
+            let io = Box::new(StdIO {}) as Box<dyn EvalIO>;
+            let res = nix_search_path.resolve(&io, "tvix/src").unwrap();
             assert_eq!(
                 res.unwrap().to_path_buf(),
                 current_dir().unwrap().join("src").clean()
@@ -245,8 +245,8 @@ mod tests {
         #[test]
         fn matching_prefix() {
             let nix_search_path = NixSearchPath::from_str("/:tvix=.").unwrap();
-            let mut io = StdIO {};
-            let res = nix_search_path.resolve(&mut io, "tvix").unwrap();
+            let io = Box::new(StdIO {}) as Box<dyn EvalIO>;
+            let res = nix_search_path.resolve(&io, "tvix").unwrap();
             assert_eq!(res.unwrap().to_path_buf(), current_dir().unwrap().clean());
         }
     }