feat(interface-types) Add the Stackable::peek1 method.

This method allows to peek the last item on the stack (if any) by
reference.
This commit is contained in:
Ivan Enderlin 2020-03-24 15:32:54 +01:00
parent 1475428a54
commit 50a6d9d92c

View File

@ -25,6 +25,10 @@ pub trait Stackable {
/// Returned items are in reverse order: the last element comes
/// last in the list.
fn pop(&mut self, n: usize) -> Option<Vec<Self::Item>>;
/// Peek the last item of the stack and returns a reference to it,
/// `None` if the stack is empty.
fn peek1(&self) -> Option<&Self::Item>;
}
/// A stack implementation of the `Stackable` trait, based on a vector.
@ -85,6 +89,14 @@ where
Some(items)
}
}
fn peek1(&self) -> Option<&Self::Item> {
if self.inner.is_empty() {
None
} else {
Some(&self.inner[self.inner.len() - 1])
}
}
}
#[cfg(test)]
@ -126,4 +138,13 @@ mod tests {
assert_eq!(stack.pop1(), None);
assert_eq!(stack.is_empty(), true);
}
#[test]
fn test_peek1() {
let mut stack = Stack::new();
stack.push(1);
stack.push(2);
assert_eq!(stack.peek1(), Some(&2));
}
}