aboutsummaryrefslogtreecommitdiff
path: root/macros/src/libs/intertrait_macros
diff options
context:
space:
mode:
Diffstat (limited to 'macros/src/libs/intertrait_macros')
-rw-r--r--macros/src/libs/intertrait_macros/gen_caster.rs40
1 files changed, 35 insertions, 5 deletions
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::<dyn #dst_trait>::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<dyn #dst_trait>)
+ },
+ |from| {
+ let concrete = from
+ .downcast::<#ty>()
+ .map_err(|_| syrette::libs::intertrait::CasterError::CastRcFailed)?;
+
+ Ok(concrete as std::rc::Rc<dyn #dst_trait>)
+ },
+ |from| {
+ let concrete = from
+ .downcast::<#ty>()
+ .map_err(|_| syrette::libs::intertrait::CasterError::CastArcFailed)?;
+
+ Ok(concrete as std::sync::Arc<dyn #dst_trait>)
+ },
)
}
} else {
quote! {
syrette::libs::intertrait::Caster::<dyn #dst_trait>::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<dyn #dst_trait>)
+ },
+ |from| {
+ let concrete = from
+ .downcast::<#ty>()
+ .map_err(|_| syrette::libs::intertrait::CasterError::CastRcFailed)?;
+
+ Ok(concrete as std::rc::Rc<dyn #dst_trait>)
+ },
)
}
};