e::new_exception!(GenericError, u32); e::new_exception!(OtherError, Box); e::new_exception!(DropError, DropTest); #[test] fn test() { let e = test2(); println!("{:?}", e); } fn test2() -> e::Result<()> { for i in 0..3 { let exception = generic(i).unwrap_err(); } for i in 0..3 { let exception = generic(i).unwrap_err(); match e::M2::from(exception) { e::M2::V0(e) if { let _: &GenericError = &e; true } => { println!("Generic: {e:?}") } e::M2::V1(e) => { let _: &OtherError = &e; println!("Other: {e:?}"); } error => error?, } } Ok(()) } fn generic(i: u32) -> e::Result<()> { match i { 0 => Err(GenericError(0x69u32))?, 1 => Err(OtherError(Box::new("Hello World".to_string())))?, 2 => Err(DropError(DropTest))?, _ => unreachable!() } } #[derive(Debug)] struct DropTest; impl Drop for DropTest { fn drop(&mut self) { println!("Dropped @ {self:p}"); } }