mirror of
https://github.com/fluencelabs/interface-types
synced 2024-12-04 15:20:20 +00:00
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:
parent
1475428a54
commit
50a6d9d92c
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user