aboutsummaryrefslogtreecommitdiff
path: root/src/libs/intertrait/cast
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/intertrait/cast')
-rw-r--r--src/libs/intertrait/cast/arc.rs13
-rw-r--r--src/libs/intertrait/cast/box.rs13
-rw-r--r--src/libs/intertrait/cast/error.rs7
-rw-r--r--src/libs/intertrait/cast/rc.rs13
4 files changed, 28 insertions, 18 deletions
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<CastFromSelf: ?Sized + CastFromSync> CastArc for CastFromSelf
self: Arc<Self>,
) -> Result<Arc<OtherTrait>, CastError>
{
- let caster =
- get_caster::<OtherTrait>((*self).type_id()).ok_or(CastError::CastFailed {
- from: type_name::<CastFromSelf>(),
- to: type_name::<OtherTrait>(),
- })?;
+ let caster = get_caster::<OtherTrait>((*self).type_id())
+ .map_err(CastError::GetCasterFailed)?;
let cast_arc = caster
.opt_cast_arc
.ok_or(CastError::NotArcCastable(type_name::<OtherTrait>()))?;
- Ok(cast_arc(self.arc_any()))
+ cast_arc(self.arc_any()).map_err(|err| CastError::CastFailed {
+ source: err,
+ from: type_name::<Self>(),
+ to: type_name::<OtherTrait>(),
+ })
}
}
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<CastFromSelf: ?Sized + CastFrom> CastBox for CastFromSelf
self: Box<Self>,
) -> Result<Box<OtherTrait>, CastError>
{
- let caster =
- get_caster::<OtherTrait>((*self).type_id()).ok_or(CastError::CastFailed {
- from: type_name::<CastFromSelf>(),
- to: type_name::<OtherTrait>(),
- })?;
+ let caster = get_caster::<OtherTrait>((*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::<Self>(),
+ to: type_name::<OtherTrait>(),
+ })
}
}
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<CastFromSelf: ?Sized + CastFrom> CastRc for CastFromSelf
self: Rc<Self>,
) -> Result<Rc<OtherTrait>, CastError>
{
- let caster =
- get_caster::<OtherTrait>((*self).type_id()).ok_or(CastError::CastFailed {
- from: type_name::<CastFromSelf>(),
- to: type_name::<OtherTrait>(),
- })?;
+ let caster = get_caster::<OtherTrait>((*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::<Self>(),
+ to: type_name::<OtherTrait>(),
+ })
}
}