From 49653bf413891b629130a609e0b33ac4155b7637 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 3 May 2019 14:15:28 +0200 Subject: Avoid unnecessary RefCell::borrow_mut --- dhall/src/normalize.rs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'dhall/src/normalize.rs') diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index cff7fdc..5749989 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -1002,9 +1002,11 @@ mod thunk { } fn normalize_nf(&mut self) { - self.normalize_whnf(); match self { - ThunkInternal::Unnormalized(_, _) => unreachable!(), + ThunkInternal::Unnormalized(_, _) => { + self.normalize_whnf(); + self.normalize_nf(); + } ThunkInternal::Value(m @ WHNF, v) => { v.normalize_mut(); *m = NF; @@ -1091,17 +1093,42 @@ mod thunk { } } + fn do_normalize_whnf(&self) { + let borrow = self.0.borrow(); + match &*borrow { + ThunkInternal::Unnormalized(_, _) => { + drop(borrow); + self.0.borrow_mut().normalize_whnf(); + } + // Already at least in WHNF + ThunkInternal::Value(_, _) => {} + } + } + + fn do_normalize_nf(&self) { + let borrow = self.0.borrow(); + match &*borrow { + ThunkInternal::Unnormalized(_, _) + | ThunkInternal::Value(WHNF, _) => { + drop(borrow); + self.0.borrow_mut().normalize_nf(); + } + // Already in NF + ThunkInternal::Value(NF, _) => {} + } + } + // WARNING: avoid normalizing any thunk while holding on to this ref - // or you will run into BorrowMut panics + // or you could run into BorrowMut panics pub(crate) fn normalize_whnf(&self) -> Ref { - self.0.borrow_mut().normalize_whnf(); + self.do_normalize_whnf(); Ref::map(self.0.borrow(), ThunkInternal::as_whnf) } // WARNING: avoid normalizing any thunk while holding on to this ref - // or you will run into BorrowMut panics + // or you could run into BorrowMut panics pub(crate) fn normalize_nf(&self) -> Ref { - self.0.borrow_mut().normalize_nf(); + self.do_normalize_nf(); Ref::map(self.0.borrow(), ThunkInternal::as_nf) } -- cgit v1.2.3