This commit is contained in:
Jef 2018-06-18 13:16:47 +00:00 committed by GitHub
commit 29493ffb7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 7 deletions

View File

@ -1,5 +1,4 @@
use std::collections::VecDeque;
use std::error;
use std::fmt;
@ -22,7 +21,7 @@ impl error::Error for Error {
#[derive(Debug)]
pub struct StackWithLimit<T> where T: Clone {
/// Stack values.
values: VecDeque<T>,
values: Vec<T>,
/// Stack limit (maximal stack len).
limit: usize,
}
@ -30,7 +29,7 @@ pub struct StackWithLimit<T> where T: Clone {
impl<T> StackWithLimit<T> where T: Clone {
pub fn with_limit(limit: usize) -> Self {
StackWithLimit {
values: VecDeque::new(),
values: Vec::new(),
limit: limit
}
}
@ -49,13 +48,13 @@ impl<T> StackWithLimit<T> where T: Clone {
pub fn top(&self) -> Result<&T, Error> {
self.values
.back()
.last()
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn top_mut(&mut self) -> Result<&mut T, Error> {
self.values
.back_mut()
.last_mut()
.ok_or_else(|| Error("non-empty stack expected".into()))
}
@ -72,13 +71,13 @@ impl<T> StackWithLimit<T> where T: Clone {
return Err(Error(format!("exceeded stack limit {}", self.limit)));
}
self.values.push_back(value);
self.values.push(value);
Ok(())
}
pub fn pop(&mut self) -> Result<T, Error> {
self.values
.pop_back()
.pop()
.ok_or_else(|| Error("non-empty stack expected".into()))
}