From 9e8ae42b2742e27a70a7fb8ea79ae21060d43fc1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 2 Nov 2020 03:47:15 +0000 Subject: Normalize `with` by mutation. This is Cow-style mutation: we clone only what's shared and then mutate it. This it more legible and more efficient than the immutable version. --- dhall/src/semantics/nze/lazy.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'dhall/src/semantics/nze/lazy.rs') diff --git a/dhall/src/semantics/nze/lazy.rs b/dhall/src/semantics/nze/lazy.rs index d361313..550f69a 100644 --- a/dhall/src/semantics/nze/lazy.rs +++ b/dhall/src/semantics/nze/lazy.rs @@ -35,6 +35,22 @@ where let _ = lazy.tgt.set(tgt); lazy } + + pub fn force(&self) -> &Tgt { + self.tgt.get_or_init(|| { + let src = self.src.take().unwrap(); + src.eval() + }) + } + + pub fn get_mut(&mut self) -> &mut Tgt { + self.force(); + self.tgt.get_mut().unwrap() + } + pub fn into_inner(self) -> Tgt { + self.force(); + self.tgt.into_inner().unwrap() + } } impl Deref for Lazy @@ -43,10 +59,7 @@ where { type Target = Tgt; fn deref(&self) -> &Self::Target { - self.tgt.get_or_init(|| { - let src = self.src.take().unwrap(); - src.eval() - }) + self.force() } } -- cgit v1.2.3 From ecc5242463308c16f38dbd5015b9f264f990b76a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Nov 2020 20:35:22 +0000 Subject: Remove NirInternal --- dhall/src/semantics/nze/lazy.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'dhall/src/semantics/nze/lazy.rs') diff --git a/dhall/src/semantics/nze/lazy.rs b/dhall/src/semantics/nze/lazy.rs index 550f69a..d3b5c8d 100644 --- a/dhall/src/semantics/nze/lazy.rs +++ b/dhall/src/semantics/nze/lazy.rs @@ -63,6 +63,17 @@ where } } +/// This implementation evaluates before cloning, because we can't clone the contents of a `Cell`. +impl Clone for Lazy +where + Src: Eval, + Tgt: Clone, +{ + fn clone(&self) -> Self { + Self::new_completed(self.force().clone()) + } +} + impl Debug for Lazy where Tgt: Debug, -- cgit v1.2.3