From 2277e62c2df6f547254f7101039c0879b0e4502b Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 18 Feb 2024 15:10:18 +0700 Subject: refactor(tvix/eval): add API for enabling impure evaluation features 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 Autosubmit: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/lib.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'tvix/eval') diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index 5d854fcc5f07..5bb48baf055d 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> { - #[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, 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, 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>) { + self.io_handle = io.unwrap_or_else(|| Box::new(StdIO) as Box); + 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> -- cgit 1.4.1