Guest User

Untitled

a guest
Oct 17th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. extern crate nodrop; // 0.1.12
  2.  
  3. use nodrop::NoDrop;
  4. use std::ops::Drop;
  5.  
  6. struct Precious;
  7. fn cleanup(_: Precious) { println!("cleaned up"); }
  8.  
  9. struct Abc(NoDrop<Precious>);
  10. struct AbcDroppable(Option<Precious>);
  11.  
  12. impl Abc {
  13. fn make_droppable(self) -> AbcDroppable {
  14. self.into()
  15. }
  16. }
  17.  
  18. impl From<Abc> for AbcDroppable {
  19. fn from(a: Abc) -> Self {
  20. AbcDroppable(Some(a.0.into_inner()))
  21. }
  22. }
  23.  
  24. impl Drop for AbcDroppable {
  25. fn drop(&mut self) {
  26. cleanup(self.0.take().unwrap()) // Should abort or something? Do we want to panic in drop?
  27. }
  28. }
  29.  
  30.  
  31. fn main() {
  32. let abc = Abc(NoDrop::new(Precious)); // init our struct.
  33.  
  34. abc.make_droppable();
  35. }
Add Comment
Please, Sign In to add comment