Guest User

Untitled

a guest
May 16th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  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`.
  27. impl Drop for ConsInternals {
  28. fn drop(&mut self) {
  29. let mut list = self.right.take();
  30. while let Value::Cons(head) = list {
  31. if let Ok(mut head) = Rc::try_unwrap(head.0) {
  32. list = head.right.take();
  33. } else {
  34. break
  35. }
  36. }
  37. }
  38. }
  39.  
  40. fn main() {}
Add Comment
Please, Sign In to add comment