These are chat archives for rust-lang/rust

26th
Aug 2016
Linda_pp
@rhysd
Aug 26 2016 11:33
I could resolve above by myself.
Pete Hayes
@petehayes102
Aug 26 2016 14:03
Hey guys. Is it possible to get a proper value back from catch_unwind(), rather than a string describing the panic?
For example, this code gives me None when I call downcast_ref(), rather than the Error enum I was hoping for:
use std::panic;

fn main() {
    let r = panic::catch_unwind(|| breaks());
    if let Err(e) = r {
        println!("{:?}", e.downcast_ref::<Error>().unwrap());
    }
}

fn breaks() {
    let e: Result<(), Error> = Err(Error::Moo("cow"));
    e.unwrap();
}

#[derive(Debug)]
enum Error {
    Moo(&'static str),
}
Thomas Koehler
@Bastacyclop
Aug 26 2016 17:46
@petehayes102 maybe you need to use stuff like https://doc.rust-lang.org/std/panic/fn.resume_unwind.html ?
Pete Hayes
@petehayes102
Aug 26 2016 17:54
Hey @Bastacyclop. Thanks for the suggestion. I asked on IRC and was pointed in the direction of the manual for Result::unwrap. Turns out that unwrap panics with a string describing the error, rather than the error itself.
Once I changed the code to explicitly panic rather than unwrap, I started getting the value I needed
Thomas Koehler
@Bastacyclop
Aug 26 2016 17:55
nice
Pete Hayes
@petehayes102
Aug 26 2016 17:57
Thanks for the help anyway :)