summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2021-10-31 12:26:03 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2021-10-31 20:19:43 +0100
commita762d729d3f07591d2fa26c154255ab43de1ad9c (patch)
tree75506015b188e7992350e7f5a2ff031bc5026427
parente3119a77bfe4be98b721dfe262b01e2c328c3c79 (diff)
downloadrebel-a762d729d3f07591d2fa26c154255ab43de1ad9c.tar
rebel-a762d729d3f07591d2fa26c154255ab43de1ad9c.zip
runner: add Stack type
A Stack guarantees that elements are dropped in reverse push order.
-rw-r--r--crates/runner/src/util/mod.rs1
-rw-r--r--crates/runner/src/util/stack.rs25
2 files changed, 26 insertions, 0 deletions
diff --git a/crates/runner/src/util/mod.rs b/crates/runner/src/util/mod.rs
index 1f89592..0fbe3b5 100644
--- a/crates/runner/src/util/mod.rs
+++ b/crates/runner/src/util/mod.rs
@@ -2,5 +2,6 @@ pub mod checkable;
pub mod cjson;
pub mod clone;
pub mod fs;
+pub mod stack;
pub mod steal;
pub mod unix;
diff --git a/crates/runner/src/util/stack.rs b/crates/runner/src/util/stack.rs
new file mode 100644
index 0000000..15d5daf
--- /dev/null
+++ b/crates/runner/src/util/stack.rs
@@ -0,0 +1,25 @@
+use std::mem;
+
+/// Simple inefficient datastructure with guaranteed drop order
+#[derive(Debug)]
+pub enum Stack<T> {
+ Cons(Box<(T, Stack<T>)>),
+ Empty,
+}
+
+impl<T> Default for Stack<T> {
+ fn default() -> Self {
+ Self::Empty
+ }
+}
+
+impl<T> Stack<T> {
+ pub fn new() -> Self {
+ Self::Empty
+ }
+
+ pub fn push(&mut self, value: T) {
+ let tmp = mem::take(self);
+ *self = Stack::Cons(Box::new((value, tmp)));
+ }
+}