Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(dead_code)]
- use std::ops::Drop;
- use std::rc::Rc;
- enum Value {
- Number(i64),
- Cons(Cons),
- Null,
- }
- impl Value {
- fn take(&mut self) -> Value {
- std::mem::replace(self, Value::Null)
- }
- }
- struct Cons(Rc<ConsInternals>);
- struct ConsInternals {
- left: Value,
- right: Value,
- }
- // Putting `Drop` on the internal representation so we don't
- // have to reach through an `Rc`.
- impl Drop for ConsInternals {
- fn drop(&mut self) {
- let mut list = self.right.take();
- while let Value::Cons(head) = list {
- if let Ok(mut head) = Rc::try_unwrap(head.0) {
- list = head.right.take();
- } else {
- break
- }
- }
- }
- }
- fn main() {}
Add Comment
Please, Sign In to add comment