Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. struct Foo;
  2.  
  3. impl Foo {
  4. fn cleanup (self: &'_ mut Self)
  5. {
  6. panic!("Foo::cleanup({:p})", self);
  7. }
  8. }
  9.  
  10. struct Struct {
  11. foo: Foo,
  12. }
  13.  
  14. impl Drop for Struct {
  15. fn drop (self: &'_ mut Self)
  16. {
  17. use ::std::panic;
  18. let mut foo = panic::AssertUnwindSafe(&mut self.foo);
  19. match panic::catch_unwind(move || {
  20. foo.cleanup();
  21. })
  22. {
  23. | Ok(()) => {},
  24.  
  25. | Err(err) => {
  26. eprintln!("{:p}.foo.cleanup() panicked ({err_msg})", self,
  27. err_msg = if let Some(s) = err.downcast_ref::<String>() {
  28. s as &dyn ::core::fmt::Display
  29. } else {
  30. &""
  31. },
  32. );
  33. },
  34. }
  35. }
  36. }
  37.  
  38. fn main ()
  39. {
  40. let _struct = Struct { foo: Foo, };
  41. if true { panic!("First panic!"); }
  42. println!("Program keeps going");
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement