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 ++++++++++-- src/di_container/asynchronous/mod.rs | 27 +++++--- src/libs/intertrait/cast/arc.rs | 13 ++-- src/libs/intertrait/cast/box.rs | 13 ++-- src/libs/intertrait/cast/error.rs | 7 +++ src/libs/intertrait/cast/rc.rs | 13 ++-- src/libs/intertrait/mod.rs | 82 ++++++++++++++++++++----- 7 files changed, 148 insertions(+), 47 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::::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) + }, ) } }; diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 7dda1d7..89b2fba 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -270,7 +270,12 @@ impl AsyncDIContainer >( )) } - CastError::CastFailed { from: _, to: _ } => { + CastError::CastFailed { + source: _, + from: _, + to: _, + } + | CastError::GetCasterFailed(_) => { AsyncDIContainerError::CastFailed { interface: type_name::(), binding_kind: "singleton", @@ -291,7 +296,12 @@ impl AsyncDIContainer type_name::(), ) } - CastError::CastFailed { from: _, to: _ } => { + CastError::CastFailed { + source: _, + from: _, + to: _, + } + | CastError::GetCasterFailed(_) => { AsyncDIContainerError::CastFailed { interface: type_name::(), binding_kind: "factory", @@ -348,12 +358,15 @@ impl AsyncDIContainer CastError::NotArcCastable(_) => { AsyncDIContainerError::InterfaceNotAsync(type_name::()) } - CastError::CastFailed { from: _, to: _ } => { - AsyncDIContainerError::CastFailed { - interface: type_name::(), - binding_kind, - } + CastError::CastFailed { + source: _, + from: _, + to: _, } + | CastError::GetCasterFailed(_) => AsyncDIContainerError::CastFailed { + interface: type_name::(), + binding_kind, + }, }) } diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs index 1742c32..135cf64 100644 --- a/src/libs/intertrait/cast/arc.rs +++ b/src/libs/intertrait/cast/arc.rs @@ -31,16 +31,17 @@ impl CastArc for CastFromSelf self: Arc, ) -> Result, CastError> { - let caster = - get_caster::((*self).type_id()).ok_or(CastError::CastFailed { - from: type_name::(), - to: type_name::(), - })?; + let caster = get_caster::((*self).type_id()) + .map_err(CastError::GetCasterFailed)?; let cast_arc = caster .opt_cast_arc .ok_or(CastError::NotArcCastable(type_name::()))?; - Ok(cast_arc(self.arc_any())) + cast_arc(self.arc_any()).map_err(|err| CastError::CastFailed { + source: err, + from: type_name::(), + to: type_name::(), + }) } } diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs index 5694d97..67fd949 100644 --- a/src/libs/intertrait/cast/box.rs +++ b/src/libs/intertrait/cast/box.rs @@ -30,12 +30,13 @@ impl CastBox for CastFromSelf self: Box, ) -> Result, CastError> { - let caster = - get_caster::((*self).type_id()).ok_or(CastError::CastFailed { - from: type_name::(), - to: type_name::(), - })?; + let caster = get_caster::((*self).type_id()) + .map_err(CastError::GetCasterFailed)?; - Ok((caster.cast_box)(self.box_any())) + (caster.cast_box)(self.box_any()).map_err(|err| CastError::CastFailed { + source: err, + from: type_name::(), + to: type_name::(), + }) } } diff --git a/src/libs/intertrait/cast/error.rs b/src/libs/intertrait/cast/error.rs index a834c05..e6d86a5 100644 --- a/src/libs/intertrait/cast/error.rs +++ b/src/libs/intertrait/cast/error.rs @@ -1,9 +1,16 @@ +use crate::libs::intertrait::{CasterError, GetCasterError}; + #[derive(thiserror::Error, Debug)] pub enum CastError { + #[error("Failed to get caster")] + GetCasterFailed(#[from] GetCasterError), + #[error("Failed to cast from trait {from} to trait {to}")] CastFailed { + #[source] + source: CasterError, from: &'static str, to: &'static str, }, diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs index 805bcd7..ec70544 100644 --- a/src/libs/intertrait/cast/rc.rs +++ b/src/libs/intertrait/cast/rc.rs @@ -30,12 +30,13 @@ impl CastRc for CastFromSelf self: Rc, ) -> Result, CastError> { - let caster = - get_caster::((*self).type_id()).ok_or(CastError::CastFailed { - from: type_name::(), - to: type_name::(), - })?; + let caster = get_caster::((*self).type_id()) + .map_err(CastError::GetCasterFailed)?; - Ok((caster.cast_rc)(self.rc_any())) + (caster.cast_rc)(self.rc_any()).map_err(|err| CastError::CastFailed { + source: err, + from: type_name::(), + to: type_name::(), + }) } } diff --git a/src/libs/intertrait/mod.rs b/src/libs/intertrait/mod.rs index dc0f19e..78f98b1 100644 --- a/src/libs/intertrait/mod.rs +++ b/src/libs/intertrait/mod.rs @@ -56,7 +56,12 @@ static CASTER_MAP: Lazy> = Lazy::new(|| .collect() }); -type CastArcFn = fn(from: Arc) -> Arc; +type CastBoxFn = fn(from: Box) -> Result, CasterError>; + +type CastRcFn = fn(from: Rc) -> Result, CasterError>; + +type CastArcFn = + fn(from: Arc) -> Result, CasterError>; /// A `Caster` knows how to cast a reference to or `Box` of a trait object for `Any` /// to a trait object of trait `Trait`. Each `Caster` instance is specific to a concrete @@ -66,11 +71,11 @@ pub struct Caster { /// Casts a `Box` holding a trait object for `Any` to another `Box` holding a trait /// object for trait `Trait`. - pub cast_box: fn(from: Box) -> Box, + pub cast_box: CastBoxFn, /// Casts an `Rc` holding a trait object for `Any` to another `Rc` holding a trait /// object for trait `Trait`. - pub cast_rc: fn(from: Rc) -> Rc, + pub cast_rc: CastRcFn, /// Casts an `Arc` holding a trait object for `Any + Sync + Send + 'static` /// to another `Arc` holding a trait object for trait `Trait`. @@ -79,10 +84,7 @@ pub struct Caster impl Caster { - pub fn new( - cast_box: fn(from: Box) -> Box, - cast_rc: fn(from: Rc) -> Rc, - ) -> Caster + pub fn new(cast_box: CastBoxFn, cast_rc: CastRcFn) -> Caster { Caster:: { cast_box, @@ -93,9 +95,9 @@ impl Caster #[allow(clippy::similar_names)] pub fn new_sync( - cast_box: fn(from: Box) -> Box, - cast_rc: fn(from: Rc) -> Rc, - cast_arc: fn(from: Arc) -> Arc, + cast_box: CastBoxFn, + cast_rc: CastRcFn, + cast_arc: CastArcFn, ) -> Caster { Caster:: { @@ -106,14 +108,42 @@ impl Caster } } +#[derive(Debug, thiserror::Error)] +pub enum CasterError +{ + #[error("Failed to cast Box")] + CastBoxFailed, + + #[error("Failed to cast Rc")] + CastRcFailed, + + #[error("Failed to cast Arc")] + CastArcFailed, +} + /// Returns a `Caster` from a concrete type `S` to a trait `Trait` implemented /// by it. -fn get_caster(type_id: TypeId) - -> Option<&'static Caster> +fn get_caster( + type_id: TypeId, +) -> Result<&'static Caster, GetCasterError> { - CASTER_MAP + let any_caster = CASTER_MAP .get(&(type_id, TypeId::of::>())) - .and_then(|caster| caster.downcast_ref::>()) + .ok_or(GetCasterError::NotFound)?; + + any_caster + .downcast_ref::>() + .ok_or(GetCasterError::DowncastFailed) +} + +#[derive(Debug, thiserror::Error)] +pub enum GetCasterError +{ + #[error("Caster not found")] + NotFound, + + #[error("Failed to downcast caster")] + DowncastFailed, } /// `CastFrom` must be extended by a trait that wants to allow for casting into another @@ -237,9 +267,27 @@ mod tests { let type_id = TypeId::of::(); let caster = Box::new(Caster:: { - cast_box: |from| from.downcast::().unwrap(), - cast_rc: |from| from.downcast::().unwrap(), - opt_cast_arc: Some(|from| from.downcast::().unwrap()), + cast_box: |from| { + let concrete = from + .downcast::() + .map_err(|_| CasterError::CastBoxFailed)?; + + Ok(concrete as Box) + }, + cast_rc: |from| { + let concrete = from + .downcast::() + .map_err(|_| CasterError::CastRcFailed)?; + + Ok(concrete as Rc) + }, + opt_cast_arc: Some(|from| { + let concrete = from + .downcast::() + .map_err(|_| CasterError::CastArcFailed)?; + + Ok(concrete as Arc) + }), }); (type_id, caster) } -- cgit v1.2.3-18-g5258