Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use failure::{Error, Fail};
- use std::any::Any;
- use std::fmt;
- use std::sync::Mutex;
- struct SyncBoxedAny(Mutex<Box<dyn Any + Send>>);
- impl SyncBoxedAny {
- pub fn new(inner: Box<dyn Any + Send>) -> Self {
- SyncBoxedAny(Mutex::new(inner))
- }
- }
- impl fmt::Display for SyncBoxedAny {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let inner = self.0.lock().unwrap();
- if let Some(x) = inner.downcast_ref::<&str>() {
- x.fmt(f)?;
- } else if let Some(x) = inner.downcast_ref::<String>() {
- x.fmt(f)?;
- }
- Ok(())
- }
- }
- impl fmt::Debug for SyncBoxedAny {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- let inner = self.0.lock().unwrap();
- if let Some(x) = inner.downcast_ref::<&str>() {
- x.fmt(f)?;
- } else if let Some(x) = inner.downcast_ref::<String>() {
- x.fmt(f)?;
- }
- Ok(())
- }
- }
- impl Fail for SyncBoxedAny {}
- fn main() -> Result<(), Error> {
- let t = std::thread::spawn(|| panic!("message"));
- t.join().map_err(SyncBoxedAny::new)?;
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement