about summary refs log tree commit diff
path: root/tvix/eval/src
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@tvl.su>2024-02-18T08·10+0700
committerclbot <clbot@tvl.fyi>2024-02-20T08·46+0000
commit2277e62c2df6f547254f7101039c0879b0e4502b (patch)
tree7bcea196fb0a10b5bea4741c26cbf17308b6abd4 /tvix/eval/src
parentdb26cb21322b59d6d205360fb8e654605dd5fdcb (diff)
refactor(tvix/eval): add API for enabling impure evaluation features r/7569
This makes some of the work of configuring an arbitrary I/O handler
easier.

Change-Id: I158db3235fe83df6e709578ed515e0e028c20086
Reviewed-on: https://cl.tvl.fyi/c/depot/+/10959
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src')
-rw-r--r--tvix/eval/src/lib.rs30
1 files changed, 20 insertions, 10 deletions
diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs
index 5d854fcc5f..5bb48baf05 100644
--- a/tvix/eval/src/lib.rs
+++ b/tvix/eval/src/lib.rs
@@ -155,21 +155,31 @@ where
 }
 
 impl<'co, 'ro> Evaluation<'co, 'ro, Box<dyn EvalIO>> {
-    #[cfg(feature = "impure")]
-    /// Initialise an `Evaluation`, with all impure features turned on by default.
-    pub fn new_impure() -> Self {
-        let mut eval = Self::new(Box::new(StdIO) as Box<dyn EvalIO>, true);
-
-        eval.builtins.extend(builtins::impure_builtins());
-
-        eval
-    }
-
     /// Initialize an `Evaluation`, without the import statement available, and
     /// all IO operations stubbed out.
     pub fn new_pure() -> Self {
         Self::new(Box::new(DummyIO) as Box<dyn EvalIO>, false)
     }
+
+    #[cfg(feature = "impure")]
+    /// Configure an `Evaluation` to have impure features available
+    /// with the given I/O implementation.
+    ///
+    /// If no I/O implementation is supplied, [`StdIO`] is used by
+    /// default.
+    pub fn enable_impure(&mut self, io: Option<Box<dyn EvalIO>>) {
+        self.io_handle = io.unwrap_or_else(|| Box::new(StdIO) as Box<dyn EvalIO>);
+        self.enable_import = true;
+        self.builtins.extend(builtins::impure_builtins());
+    }
+
+    #[cfg(feature = "impure")]
+    /// Initialise an `Evaluation`, with all impure features turned on by default.
+    pub fn new_impure() -> Self {
+        let mut eval = Self::new_pure();
+        eval.enable_impure(None);
+        eval
+    }
 }
 
 impl<'co, 'ro, IO> Evaluation<'co, 'ro, IO>