From c2279c63912a47bf3078f5df3b3156ba0d9afe9f Mon Sep 17 00:00:00 2001 From: SoniEx2 Date: Tue, 23 Mar 2021 12:25:10 -0300 Subject: Clean up RAII transactions, with impl_trait crate --- src/parser.rs | 120 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 63 insertions(+), 57 deletions(-) (limited to 'src/parser.rs') diff --git a/src/parser.rs b/src/parser.rs index 8f8c200..ff3407a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -20,6 +20,7 @@ use std::borrow::Borrow; use std::collections::BTreeMap; use std::mem::ManuallyDrop; +use impl_trait::impl_trait; use regex::Regex; use crate::PatternTypes; @@ -28,6 +29,7 @@ use crate::errors::PatternError; use crate::vm::PatternConstants; use crate::vm::PatternElement; + /// try! with bools. (the b comes from bool.) macro_rules! bry { ($l:lifetime $e:expr) => { @@ -79,41 +81,43 @@ struct SubtreeHelper<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: Patt root: &'r mut Parser<'s, P, O, T>, } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> SubtreeHelper<'r, 's, P, O, T> where Self: 'r { - fn start(value: &'r mut Parser<'s, P, O, T>) -> Self { - value.consts.protos.push(Default::default()); - Self { - root: value, +impl_trait! { + impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> SubtreeHelper<'r, 's, P, O, T> where Self: 'r { + fn start(value: &'r mut Parser<'s, P, O, T>) -> Self { + value.consts.protos.push(Default::default()); + Self { + root: value, + } } - } - fn commit(self) -> usize { - let mut self_ = ManuallyDrop::new(self); - let proto = self_.root.consts.protos.pop().unwrap(); - let id = self_.root.closed_subtrees.next().unwrap(); - self_.root.consts.protos.insert(id, proto); - id - } -} + fn commit(self) -> usize { + let mut self_ = ManuallyDrop::new(self); + let proto = self_.root.consts.protos.pop().unwrap(); + let id = self_.root.closed_subtrees.next().unwrap(); + self_.root.consts.protos.insert(id, proto); + id + } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> std::ops::Deref for SubtreeHelper<'r, 's, P, O, T> where Self: 'r { - type Target = Parser<'s, P, O, T>; + impl trait std::ops::Deref { + type Target = Parser<'s, P, O, T>; - fn deref(&self) -> &Self::Target { - &*self.root - } -} + fn deref(&self) -> &Self::Target { + &*self.root + } + } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> std::ops::DerefMut for SubtreeHelper<'r, 's, P, O, T> where Self: 'r { - fn deref_mut(&mut self) -> &mut Self::Target { - self.root - } -} + impl trait std::ops::DerefMut { + fn deref_mut(&mut self) -> &mut Self::Target { + self.root + } + } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> Drop for SubtreeHelper<'r, 's, P, O, T> where Self: 'r { - fn drop(&mut self) { - // remove "partial" proto - self.root.consts.protos.pop().expect("SubtreeHelper"); + impl trait Drop { + fn drop(&mut self) { + // remove "partial" proto + self.root.consts.protos.pop().expect("SubtreeHelper"); + } + } } } @@ -122,40 +126,42 @@ struct TagHelper<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternT len: usize, } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> TagHelper<'r, 's, P, O, T> where Self: 'r { - fn start(value: &'r mut Parser<'s, P, O, T>) -> Self { - let len = value.consts.protos.last().unwrap().len(); - Self { - root: value, - len : len, +impl_trait! { + impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> TagHelper<'r, 's, P, O, T> where Self: 'r { + fn start(value: &'r mut Parser<'s, P, O, T>) -> Self { + let len = value.consts.protos.last().unwrap().len(); + Self { + root: value, + len : len, + } } - } - fn commit(self) { - std::mem::forget(self) - } -} + fn commit(self) { + std::mem::forget(self) + } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> std::ops::Deref for TagHelper<'r, 's, P, O, T> where Self: 'r { - type Target = Parser<'s, P, O, T>; + impl trait std::ops::Deref { + type Target = Parser<'s, P, O, T>; - fn deref(&self) -> &Self::Target { - &*self.root - } -} + fn deref(&self) -> &Self::Target { + &*self.root + } + } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> std::ops::DerefMut for TagHelper<'r, 's, P, O, T> where Self: 'r { - fn deref_mut(&mut self) -> &mut Self::Target { - self.root - } -} + impl trait std::ops::DerefMut { + fn deref_mut(&mut self) -> &mut Self::Target { + self.root + } + } -impl<'r, 's, P: Borrow + Ord, O: Borrow + Ord, T: PatternTypes> Drop for TagHelper<'r, 's, P, O, T> where Self: 'r { - fn drop(&mut self) { - let proto = self.root.consts.protos.last_mut().unwrap(); - assert!(proto.len() >= self.len); - while proto.len() > self.len { - let _ = proto.pop(); + impl trait Drop { + fn drop(&mut self) { + let proto = self.root.consts.protos.last_mut().unwrap(); + assert!(proto.len() >= self.len); + while proto.len() > self.len { + let _ = proto.pop(); + } + } } } } -- cgit 1.4.1