sfackler / rust-log-panics

Apache License 2.0
52 stars 14 forks source link

Little code improvement #9

Open piegamesde opened 3 years ago

piegamesde commented 3 years ago

I'm too lazy to fork and PR right now, sorry ^^

I just think that

        let msg = info.payload().downcast_ref::<&'static str>()
            .or_else(|| {
                info.payload()
                    .downcast_ref::<String>()
                    .map(String::as_str)
            })
            .unwrap_or("Box<Any>");

would be a bit more idiomatic than the current nested match blocks. I haven't tested the code, but I hope you get the idea

piegamesde commented 3 years ago

Apparently there is another rather elegant way of going about this:

https://docs.rs/human-panic/1.0.3/src/human_panic/lib.rs.html#194-201

  let message = match (
    panic_info.payload().downcast_ref::<&str>(),
    panic_info.payload().downcast_ref::<String>(),
  ) {
    (Some(s), _) => Some(s.to_string()),
    (_, Some(s)) => Some(s.to_string()),
    (None, None) => None,
  };