Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little code improvement #9

Open
piegamesde opened this issue Jul 7, 2021 · 1 comment
Open

Little code improvement #9

piegamesde opened this issue Jul 7, 2021 · 1 comment

Comments

@piegamesde
Copy link

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
Copy link
Author

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,
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant