diff --git a/src/mango/io/fortest/stringreader.rs b/src/mango/io/fortest/stringreader.rs index fd5a1d4c..9419fb5a 100644 --- a/src/mango/io/fortest/stringreader.rs +++ b/src/mango/io/fortest/stringreader.rs @@ -52,6 +52,10 @@ impl Reader for StringReader { }; }) } + + fn get_progress(&self) -> usize { + self.index + } } // TODO: tests (spaces, end) diff --git a/src/mango/io/typ.rs b/src/mango/io/typ.rs index 5f8fd9f0..6aedf0d5 100644 --- a/src/mango/io/typ.rs +++ b/src/mango/io/typ.rs @@ -17,6 +17,10 @@ pub trait Reader: Debug { /// /// This has to eventually return EOF, and keep returning EOF forever after that. fn matches(&mut self, subpattern: &str) -> ReaderResult; + + /// Return a number that can be used to check whether the state has changed. + /// This need not correspond to a specific position, but should be unique for the progress. + fn get_progress(&self) -> usize; } pub trait Writer { diff --git a/src/mango/util/collection/mod.rs b/src/mango/util/collection/mod.rs index 5f327a57..f31304ed 100644 --- a/src/mango/util/collection/mod.rs +++ b/src/mango/util/collection/mod.rs @@ -1,2 +1,5 @@ pub mod queue; pub use self::queue::Queue; + +pub mod stack; +pub use self::stack::Stack; diff --git a/src/mango/util/collection/queue.rs b/src/mango/util/collection/queue.rs index 02996c44..8eb43ec1 100644 --- a/src/mango/util/collection/queue.rs +++ b/src/mango/util/collection/queue.rs @@ -1,23 +1,27 @@ -use std::collections::VecDeque; -/// A one-ended queue. -/// This is just a wrapper around deque so nobody pushes or pops the wrong end. +/// A one-ended queue. See also [Stack]. +/// This is just a wrapper around vec so nobody pushes or pops the wrong end. pub struct Queue { - deque: VecDeque, + items: Vec, } impl Queue { pub fn new() -> Self { Queue { - deque: VecDeque::with_capacity(16), + items: Vec::with_capacity(16), } } pub fn push(&mut self, value: T) { - self.deque.push_back(value) + self.items.push(value) } pub fn pop(&mut self) -> Option { - self.deque.pop_front() + self.items.pop() + } + + /// Moves all the elements from a vector into the queue. + pub fn append(&mut self, mut other: Vec) { + self.items.append(&mut other); } } diff --git a/src/mango/util/collection/stack.rs b/src/mango/util/collection/stack.rs new file mode 100644 index 00000000..055c0a18 --- /dev/null +++ b/src/mango/util/collection/stack.rs @@ -0,0 +1,27 @@ +use std::collections::VecDeque; + +/// A one-ended stack. See also [Queue]. +/// This is just a wrapper around deque so nobody pushes or pops the wrong end. +pub struct Stack { + items: VecDeque, +} + +impl Stack { + pub fn new() -> Self { + Stack { + items: VecDeque::with_capacity(16), + } + } + + pub fn push(&mut self, value: T) { + self.items.push_back(value) + } + + pub fn pop(&mut self) -> Option { + self.items.pop_back() + } + + pub fn borrow_mut(&mut self) -> Option<&mut T> { + self.items.back_mut() + } +}