Remove VecDeque

This commit is contained in:
Jef 2018-06-13 16:51:16 +02:00
parent f305b3cd1f
commit 7e7214d80b
1 changed files with 6 additions and 7 deletions

View File

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