Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created November 10, 2017 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimsynz/3829cb379635875cbf324a751572b6d4 to your computer and use it in GitHub Desktop.
Save jimsynz/3829cb379635875cbf324a751572b6d4 to your computer and use it in GitHub Desktop.
pub trait Object { }
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);
| ^^^
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