Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Copy, Clone)]
- struct NodeUnit;
- #[derive(Copy, Clone)]
- struct Node<S: Copy> {
- stack: S
- }
- impl NodeUnit {
- fn init() -> Node<(NodeUnit)> {
- Node { stack: (NodeUnit) }
- }
- }
- impl<S> Node<S>
- where
- S: Copy,
- {
- fn push<N: Copy>(self, next: N) -> Node<(N, S)> {
- Node { stack: (next, self.stack) }
- }
- }
- impl<C, P> Node<(C, P)>
- where
- C: Copy,
- P: Copy,
- {
- fn peek(&self) -> &C {
- &self.stack.0
- }
- fn pop(self) -> Node<P> {
- Node { stack: self.stack.1 }
- }
- }
- #[derive(Debug)]
- struct MyObject {
- value: u8,
- }
- fn main() {
- let obj1 = MyObject { value: 0 };
- let obj2 = MyObject { value: 1 };
- let n = NodeUnit::init();
- let n2 = n.push(&obj1);
- let n3 = n2.push(&obj2);
- println!("{:?}", n3.peek());
- let n2 = n3.pop();
- println!("{:?}", n2.peek());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement