Created
November 10, 2017 21:14
-
-
Save jimsynz/3829cb379635875cbf324a751572b6d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub trait Object { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
error[E0599]: no method named `wat` found for type `std::boxed::Box<object::Object>` in the current scope | |
--> src/stack.rs:57:27 | |
| | |
57 | assert_eq!(canary.wat(), 13); | |
| ^^^ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use object::Object; | |
pub struct Stack(Vec<Box<Object>>); | |
impl Stack { | |
pub fn new() -> Stack { | |
Stack(vec![]) | |
} | |
pub fn is_empty(&self) -> bool { | |
self.0.is_empty() | |
} | |
pub fn push(&mut self, value: Box<Object>) { | |
self.0.push(value); | |
} | |
pub fn pop(&mut self) -> Box<Object> { | |
match self.0.pop() { | |
Some(obj) => obj, | |
None => panic!("Unable to pop from empty stack!") | |
} | |
} | |
} | |
#[cfg(test)] | |
mod test { | |
use super::*; | |
#[derive(Debug, PartialEq)] | |
struct Canary(usize); | |
impl Object for Canary {} | |
impl Canary { | |
fn wat(&self) -> usize { | |
self.0 | |
} | |
} | |
#[test] | |
fn new() { | |
let stack = Stack::new(); | |
assert!(stack.is_empty()); | |
} | |
#[test] | |
fn push() { | |
let mut stack = Stack::new(); | |
stack.push(Box::new(Canary(13))); | |
assert!(!stack.is_empty()); | |
} | |
#[test] | |
fn pop() { | |
let mut stack = Stack::new(); | |
stack.push(Box::new(Canary(13))); | |
let canary = stack.pop(); | |
assert_eq!(canary.wat(), 13); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment