From de27ef3745148fc74b342d5e5eece61409730e16 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Thu, 14 Jun 2018 16:35:36 +0300 Subject: [PATCH] Use Vec instead of VecDeque. --- src/common/stack.rs | 19 ++++++++----------- src/runner.rs | 4 ---- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/common/stack.rs b/src/common/stack.rs index a9c2a9d..f30604c 100644 --- a/src/common/stack.rs +++ b/src/common/stack.rs @@ -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 where T: Clone { /// Stack values. - values: VecDeque, + values: Vec, /// Stack limit (maximal stack len). limit: usize, } @@ -30,7 +29,7 @@ pub struct StackWithLimit where T: Clone { impl StackWithLimit where T: Clone { pub fn with_limit(limit: usize) -> Self { StackWithLimit { - values: VecDeque::new(), + values: Vec::new(), limit: limit } } @@ -43,19 +42,17 @@ impl StackWithLimit where T: Clone { self.values.len() } - pub fn limit(&self) -> usize { - self.limit - } - pub fn top(&self) -> Result<&T, Error> { + let len = self.values.len(); self.values - .back() + .get(len - 1) .ok_or_else(|| Error("non-empty stack expected".into())) } pub fn top_mut(&mut self) -> Result<&mut T, Error> { + let len = self.values.len(); self.values - .back_mut() + .get_mut(len - 1) .ok_or_else(|| Error("non-empty stack expected".into())) } @@ -80,13 +77,13 @@ impl StackWithLimit 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 { self.values - .pop_back() + .pop() .ok_or_else(|| Error("non-empty stack expected".into())) } diff --git a/src/runner.rs b/src/runner.rs index ae8a5d1..20ab4b5 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -1216,10 +1216,6 @@ impl ValueStack { self.stack_with_limit.len() } - fn limit(&self) -> usize { - self.stack_with_limit.limit() - } - fn top(&self) -> &RuntimeValue { self.stack_with_limit.top().expect("Due to validation stack shouldn't be empty") }