From 3c993aa73c93f0fe335ced78d9709b39cdbd1935 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 6 Nov 2022 14:06:22 +0100 Subject: refactor: improve cast error handling --- macros/src/libs/intertrait_macros/gen_caster.rs | 40 +++++++++++++++++++++---- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'macros') diff --git a/macros/src/libs/intertrait_macros/gen_caster.rs b/macros/src/libs/intertrait_macros/gen_caster.rs index df743e2..a703a62 100644 --- a/macros/src/libs/intertrait_macros/gen_caster.rs +++ b/macros/src/libs/intertrait_macros/gen_caster.rs @@ -33,16 +33,46 @@ pub fn generate_caster( let new_caster = if sync { quote! { syrette::libs::intertrait::Caster::::new_sync( - |from| from.downcast::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap() + |from| { + let concrete = from + .downcast::<#ty>() + .map_err(|_| syrette::libs::intertrait::CasterError::CastBoxFailed)?; + + Ok(concrete as Box) + }, + |from| { + let concrete = from + .downcast::<#ty>() + .map_err(|_| syrette::libs::intertrait::CasterError::CastRcFailed)?; + + Ok(concrete as std::rc::Rc) + }, + |from| { + let concrete = from + .downcast::<#ty>() + .map_err(|_| syrette::libs::intertrait::CasterError::CastArcFailed)?; + + Ok(concrete as std::sync::Arc) + }, ) } } else { quote! { syrette::libs::intertrait::Caster::::new( - |from| from.downcast::<#ty>().unwrap(), - |from| from.downcast::<#ty>().unwrap(), + |from| { + let concrete = from + .downcast::<#ty>() + .map_err(|_| syrette::libs::intertrait::CasterError::CastBoxFailed)?; + + Ok(concrete as Box) + }, + |from| { + let concrete = from + .downcast::<#ty>() + .map_err(|_| syrette::libs::intertrait::CasterError::CastRcFailed)?; + + Ok(concrete as std::rc::Rc) + }, ) } }; -- cgit v1.2.3-18-g5258