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 {
- // Takes ownership of the `Value`, leaving `Value::Null` in its place.
- fn take(&mut self) -> Value {
- std::mem::replace(self, Value::Null)
- }
- fn pop(&mut self) -> Option<Value> {
- }
- }
- struct Cons(Rc<ConsInternals>);
- struct ConsInternals {
- left: Value,
- right: Value,
- }
- // Note: `Drop` is implemented on the internal representation so we don't have
- // to reach through an `Rc` to something that might not even be being dropped.
- impl Drop for ConsInternals {
- fn drop(&mut self) {
- let mut tail = self.right.take();
- while let Value::Cons(cons) = tail {
- if let Ok(mut cons_internals) = Rc::try_unwrap(cons.0) {
- tail = cons_internals.right.take();
- } else {
- break
- }
- }
- }
- }
- fn main() {}
Add Comment
Please, Sign In to add comment