From a762d729d3f07591d2fa26c154255ab43de1ad9c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 31 Oct 2021 12:26:03 +0100 Subject: runner: add Stack type A Stack guarantees that elements are dropped in reverse push order. --- crates/runner/src/util/mod.rs | 1 + crates/runner/src/util/stack.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 crates/runner/src/util/stack.rs 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 { + Cons(Box<(T, Stack)>), + Empty, +} + +impl Default for Stack { + fn default() -> Self { + Self::Empty + } +} + +impl Stack { + 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))); + } +} -- cgit v1.2.3