ok_or_else in stack + top_mut.

This commit is contained in:
Sergey Pepyakin 2018-02-07 14:47:08 +03:00
parent 528f468ef2
commit fdd527e996
1 changed files with 8 additions and 2 deletions

View File

@ -50,7 +50,13 @@ impl<T> StackWithLimit<T> where T: Clone {
pub fn top(&self) -> Result<&T, Error> {
self.values
.back()
.ok_or(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> {
self.values
.back_mut()
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn get(&self, index: usize) -> Result<&T, Error> {
@ -73,7 +79,7 @@ impl<T> StackWithLimit<T> where T: Clone {
pub fn pop(&mut self) -> Result<T, Error> {
self.values
.pop_back()
.ok_or(Error("non-empty stack expected".into()))
.ok_or_else(|| Error("non-empty stack expected".into()))
}
pub fn resize(&mut self, new_size: usize, dummy: T) {