Advertisement
Guest User

Untitled

a guest
May 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #[derive(Copy, Clone)]
  2. struct NodeUnit;
  3. #[derive(Copy, Clone)]
  4. struct Node<S: Copy> {
  5. stack: S
  6. }
  7.  
  8. impl NodeUnit {
  9. fn init() -> Node<(NodeUnit)> {
  10. Node { stack: (NodeUnit) }
  11. }
  12. }
  13.  
  14. impl<S> Node<S>
  15. where
  16. S: Copy,
  17. {
  18. fn push<N: Copy>(self, next: N) -> Node<(N, S)> {
  19. Node { stack: (next, self.stack) }
  20. }
  21. }
  22.  
  23. impl<C, P> Node<(C, P)>
  24. where
  25. C: Copy,
  26. P: Copy,
  27. {
  28. fn peek(&self) -> &C {
  29. &self.stack.0
  30. }
  31.  
  32. fn pop(self) -> Node<P> {
  33. Node { stack: self.stack.1 }
  34. }
  35. }
  36.  
  37. #[derive(Debug)]
  38. struct MyObject {
  39. value: u8,
  40. }
  41.  
  42. fn main() {
  43. let obj1 = MyObject { value: 0 };
  44. let obj2 = MyObject { value: 1 };
  45. let n = NodeUnit::init();
  46. let n2 = n.push(&obj1);
  47. let n3 = n2.push(&obj2);
  48. println!("{:?}", n3.peek());
  49.  
  50. let n2 = n3.pop();
  51. println!("{:?}", n2.peek());
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement