Guest User

Untitled

a guest
May 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 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. // Takes ownership of the `Value`, leaving `Value::Null` in its place.
  14. fn take(&mut self) -> Value {
  15. std::mem::replace(self, Value::Null)
  16. }
  17.  
  18. fn pop(&mut self) -> Option<Value> {
  19.  
  20. }
  21. }
  22.  
  23. struct Cons(Rc<ConsInternals>);
  24.  
  25. struct ConsInternals {
  26. left: Value,
  27. right: Value,
  28. }
  29.  
  30. // Note: `Drop` is implemented on the internal representation so we don't have
  31. // to reach through an `Rc` to something that might not even be being dropped.
  32. impl Drop for ConsInternals {
  33. fn drop(&mut self) {
  34. let mut tail = self.right.take();
  35. while let Value::Cons(cons) = tail {
  36. if let Ok(mut cons_internals) = Rc::try_unwrap(cons.0) {
  37. tail = cons_internals.right.take();
  38. } else {
  39. break
  40. }
  41. }
  42. }
  43. }
  44.  
  45. fn main() {}
Add Comment
Please, Sign In to add comment