Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created November 10, 2017 21:14
Show Gist options
  • Select an option

  • Save jimsynz/3829cb379635875cbf324a751572b6d4 to your computer and use it in GitHub Desktop.

Select an option

Save jimsynz/3829cb379635875cbf324a751572b6d4 to your computer and use it in GitHub Desktop.

Revisions

  1. James Harton revised this gist Nov 10, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions output.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    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);
    | ^^^
  2. James Harton created this gist Nov 10, 2017.
    1 change: 1 addition & 0 deletions object.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    pub trait Object { }
    59 changes: 59 additions & 0 deletions stack.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    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);
    }
    }