aboutsummaryrefslogtreecommitdiff
path: root/macros/src/libs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-11-06 14:06:22 +0100
committerHampusM <hampus@hampusmat.com>2022-11-06 14:06:22 +0100
commit3c993aa73c93f0fe335ced78d9709b39cdbd1935 (patch)
tree5b8d1d3bf743fe2383687d172caaee08df69a08b /macros/src/libs
parente665c2a3ca9b15f406c8e12e4a7ab372fcad4d36 (diff)
refactor: improve cast error handling
Diffstat (limited to 'macros/src/libs')
-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>)
+ },
)
}
};