daily pastebin goal
24%
SHARE
TWEET

Untitled

a guest May 16th, 2018 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![allow(dead_code)]
  2.  
  3. use std::ops::Drop;
  4. use std::rc::Rc;
  5.  
  6. enum Value {
  7.     Number(i64),
  8.     Cons(Cons),
  9.     Null,
  10. }
  11.  
  12. impl Value {
  13.     fn take(&mut self) -> Value {
  14.         std::mem::replace(self, Value::Null)
  15.     }
  16. }
  17.  
  18. struct Cons(Rc<ConsInternals>);
  19.  
  20. struct ConsInternals {
  21.     left: Value,
  22.     right: Value,
  23. }
  24.  
  25. // Putting `Drop` on the internal representation so we don't
  26. // have to reach through an `Rc` to something that might not
  27. // even be being dropped.
  28. impl Drop for ConsInternals {
  29.     fn drop(&mut self) {
  30.         let mut list = self.right.take();
  31.         while let Value::Cons(head) = list {
  32.             if let Ok(mut head) = Rc::try_unwrap(head.0) {
  33.                 list = head.right.take();
  34.             } else {
  35.                 break
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. fn main() {}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top