diff options
author | Nadrieril | 2020-11-02 03:47:15 +0000 |
---|---|---|
committer | Nadrieril | 2020-11-03 23:18:58 +0000 |
commit | 9e8ae42b2742e27a70a7fb8ea79ae21060d43fc1 (patch) | |
tree | fadbca8531f19eeafa80550ebd1e62d891a8f3e1 /dhall/src/semantics/nze/lazy.rs | |
parent | 055e70f52bb0d8740ce6ac00b98ae856c29642b2 (diff) |
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.
Diffstat (limited to '')
-rw-r--r-- | dhall/src/semantics/nze/lazy.rs | 21 |
1 files changed, 17 insertions, 4 deletions
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<Src, Tgt> Deref for Lazy<Src, Tgt> @@ -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() } } |