summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2021-03-23 12:25:10 -0300
committerSoniEx2 <endermoneymod@gmail.com>2021-03-23 12:25:10 -0300
commitc2279c63912a47bf3078f5df3b3156ba0d9afe9f (patch)
treeef1a07f67ed41012587ae5c6548c2e4eef33ee61
parent37cea5a3a1dc1a56b44b2445ea71f89035ed7327 (diff)
Clean up RAII transactions, with impl_trait crate
-rw-r--r--Cargo.toml1
-rw-r--r--src/lib.rs1
-rw-r--r--src/parser.rs120
3 files changed, 65 insertions, 57 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 1dfd690..fc15218 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,6 +13,7 @@ homepage = "https://soniex2.github.io/ganarchy/project/c0b4a8a326a320ac33c5d9d6b
 
 [dependencies]
 regex = "1"
+impl_trait = "0.1.2"
 
 [target.'cfg(target_os = "linux")'.dependencies]
 totally-safe-transmute = "0.0.3"
diff --git a/src/lib.rs b/src/lib.rs
index 3cb2198..3fc542f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -115,6 +115,7 @@
 //!
 //! <!-- TODO -->
 
+extern crate impl_trait;
 extern crate regex;
 
 #[cfg(test)]
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<str> + Ord, O: Borrow<str> + Ord, T: Patt
     root: &'r mut Parser<'s, P, O, T>,
 }
 
-impl<'r, 's, P: Borrow<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + Ord, T: PatternT
     len: usize,
 }
 
-impl<'r, 's, P: Borrow<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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<str> + Ord, O: Borrow<str> + 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();
+                }
+            }
         }
     }
 }