diff options
Diffstat (limited to 'src/libs/intertrait/cast')
-rw-r--r-- | src/libs/intertrait/cast/arc.rs | 21 | ||||
-rw-r--r-- | src/libs/intertrait/cast/box.rs | 4 | ||||
-rw-r--r-- | src/libs/intertrait/cast/error.rs | 3 | ||||
-rw-r--r-- | src/libs/intertrait/cast/rc.rs | 4 |
4 files changed, 21 insertions, 11 deletions
diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs index 94c0482..33d84d2 100644 --- a/src/libs/intertrait/cast/arc.rs +++ b/src/libs/intertrait/cast/arc.rs @@ -13,7 +13,7 @@ use std::any::type_name; use std::sync::Arc; use crate::libs::intertrait::cast::error::CastError; -use crate::libs::intertrait::{caster, CastFromSync}; +use crate::libs::intertrait::{get_caster, CastFromSync}; pub trait CastArc { @@ -31,12 +31,19 @@ impl<CastFromSelf: ?Sized + CastFromSync> CastArc for CastFromSelf self: Arc<Self>, ) -> Result<Arc<OtherTrait>, CastError> { - match caster::<OtherTrait>((*self).type_id()) { - Some(caster) => Ok((caster.cast_arc)(self.arc_any())), - None => Err(CastError::CastFailed { - from: type_name::<CastFromSelf>(), - to: type_name::<OtherTrait>(), - }), + let caster = get_caster::<OtherTrait>((*self).type_id()).map_or_else( + || { + Err(CastError::CastFailed { + from: type_name::<CastFromSelf>(), + to: type_name::<OtherTrait>(), + }) + }, + Ok, + )?; + + match caster.opt_cast_arc { + Some(cast_arc) => Ok(cast_arc(self.arc_any())), + None => Err(CastError::NotArcCastable(type_name::<OtherTrait>())), } } } diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs index 31f06db..c463c2f 100644 --- a/src/libs/intertrait/cast/box.rs +++ b/src/libs/intertrait/cast/box.rs @@ -13,7 +13,7 @@ use std::any::type_name; use crate::libs::intertrait::cast::error::CastError; -use crate::libs::intertrait::{caster, CastFrom}; +use crate::libs::intertrait::{get_caster, CastFrom}; pub trait CastBox { @@ -30,7 +30,7 @@ impl<CastFromSelf: ?Sized + CastFrom> CastBox for CastFromSelf self: Box<Self>, ) -> Result<Box<OtherTrait>, CastError> { - match caster::<OtherTrait>((*self).type_id()) { + match get_caster::<OtherTrait>((*self).type_id()) { Some(caster) => Ok((caster.cast_box)(self.box_any())), None => Err(CastError::CastFailed { from: type_name::<CastFromSelf>(), diff --git a/src/libs/intertrait/cast/error.rs b/src/libs/intertrait/cast/error.rs index 74eb3ca..a834c05 100644 --- a/src/libs/intertrait/cast/error.rs +++ b/src/libs/intertrait/cast/error.rs @@ -7,4 +7,7 @@ pub enum CastError from: &'static str, to: &'static str, }, + + #[error("Trait '{0}' can't be cast to Arc")] + NotArcCastable(&'static str), } diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs index dfb71c2..63c0024 100644 --- a/src/libs/intertrait/cast/rc.rs +++ b/src/libs/intertrait/cast/rc.rs @@ -13,7 +13,7 @@ use std::any::type_name; use std::rc::Rc; use crate::libs::intertrait::cast::error::CastError; -use crate::libs::intertrait::{caster, CastFrom}; +use crate::libs::intertrait::{get_caster, CastFrom}; pub trait CastRc { @@ -30,7 +30,7 @@ impl<CastFromSelf: ?Sized + CastFrom> CastRc for CastFromSelf self: Rc<Self>, ) -> Result<Rc<OtherTrait>, CastError> { - match caster::<OtherTrait>((*self).type_id()) { + match get_caster::<OtherTrait>((*self).type_id()) { Some(caster) => Ok((caster.cast_rc)(self.rc_any())), None => Err(CastError::CastFailed { from: type_name::<CastFromSelf>(), |