From c877e1d920f3dce5598fd50f68c05b5e876dea5f Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 5 Nov 2022 14:57:33 +0300 Subject: refactor(tvix/eval): move `unwrap_or_clone_rc` to lib module This is more generally useful than just inside the VM, until it is stabilised in Rust itself. Change-Id: Id9aa3d5b533ff38e3d2c6b85ad484394fdd05dcf Reviewed-on: https://cl.tvl.fyi/c/depot/+/7186 Tested-by: BuildkiteCI Autosubmit: tazjin Reviewed-by: grfn Reviewed-by: Adam Joseph --- tvix/eval/src/lib.rs | 8 ++++++++ tvix/eval/src/vm.rs | 7 +------ 2 files changed, 9 insertions(+), 6 deletions(-) (limited to 'tvix/eval') diff --git a/tvix/eval/src/lib.rs b/tvix/eval/src/lib.rs index 6cf3aa212a18..00f680bc7a2e 100644 --- a/tvix/eval/src/lib.rs +++ b/tvix/eval/src/lib.rs @@ -22,6 +22,8 @@ mod test_utils; #[cfg(test)] mod tests; +use std::rc::Rc; + // Re-export the public interface used by other crates. pub use crate::builtins::global_builtins; pub use crate::compiler::{compile, prepare_globals}; @@ -31,3 +33,9 @@ pub use crate::pretty_ast::pretty_print_expr; pub use crate::source::SourceCode; pub use crate::value::Value; pub use crate::vm::run_lambda; + +// TODO: use Rc::unwrap_or_clone once it is stabilised. +// https://doc.rust-lang.org/std/rc/struct.Rc.html#method.unwrap_or_clone +pub fn unwrap_or_clone_rc(rc: Rc) -> T { + Rc::try_unwrap(rc).unwrap_or_else(|rc| (*rc).clone()) +} diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs index c67e9f6d8899..8c45ee7363e1 100644 --- a/tvix/eval/src/vm.rs +++ b/tvix/eval/src/vm.rs @@ -10,6 +10,7 @@ use crate::{ nix_search_path::NixSearchPath, observer::RuntimeObserver, opcode::{CodeIdx, Count, JumpOffset, OpCode, StackIdx, UpvalueIdx}, + unwrap_or_clone_rc, upvalues::Upvalues, value::{Builtin, Closure, CoercionKind, Lambda, NixAttrs, NixList, Thunk, Value}, warnings::{EvalWarning, WarningKind}, @@ -884,12 +885,6 @@ impl<'o> VM<'o> { } } -// TODO: use Rc::unwrap_or_clone once it is stabilised. -// https://doc.rust-lang.org/std/rc/struct.Rc.html#method.unwrap_or_clone -fn unwrap_or_clone_rc(rc: Rc) -> T { - Rc::try_unwrap(rc).unwrap_or_else(|rc| (*rc).clone()) -} - pub fn run_lambda( nix_search_path: NixSearchPath, observer: &mut dyn RuntimeObserver, -- cgit 1.4.1