Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. use failure::{Error, Fail};
  2. use std::any::Any;
  3. use std::fmt;
  4. use std::sync::Mutex;
  5.  
  6. struct SyncBoxedAny(Mutex<Box<dyn Any + Send>>);
  7.  
  8. impl SyncBoxedAny {
  9. pub fn new(inner: Box<dyn Any + Send>) -> Self {
  10. SyncBoxedAny(Mutex::new(inner))
  11. }
  12. }
  13.  
  14. impl fmt::Display for SyncBoxedAny {
  15. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  16. let inner = self.0.lock().unwrap();
  17.  
  18. if let Some(x) = inner.downcast_ref::<&str>() {
  19. x.fmt(f)?;
  20. } else if let Some(x) = inner.downcast_ref::<String>() {
  21. x.fmt(f)?;
  22. }
  23.  
  24. Ok(())
  25. }
  26. }
  27.  
  28. impl fmt::Debug for SyncBoxedAny {
  29. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  30. let inner = self.0.lock().unwrap();
  31.  
  32. if let Some(x) = inner.downcast_ref::<&str>() {
  33. x.fmt(f)?;
  34. } else if let Some(x) = inner.downcast_ref::<String>() {
  35. x.fmt(f)?;
  36. }
  37.  
  38. Ok(())
  39. }
  40. }
  41.  
  42. impl Fail for SyncBoxedAny {}
  43.  
  44. fn main() -> Result<(), Error> {
  45. let t = std::thread::spawn(|| panic!("message"));
  46.  
  47. t.join().map_err(SyncBoxedAny::new)?;
  48.  
  49. Ok(())
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement