Created
November 10, 2017 21:14
-
-
Save jimsynz/3829cb379635875cbf324a751572b6d4 to your computer and use it in GitHub Desktop.
Revisions
-
James Harton revised this gist
Nov 10, 2017 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal 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); | ^^^ -
James Harton created this gist
Nov 10, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ 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 charactersOriginal 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); } }