aboutsummaryrefslogtreecommitdiff
path: root/src/libs/intertrait/cast/arc.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-30 18:53:23 +0200
committerHampusM <hampus@hampusmat.com>2022-08-30 18:53:23 +0200
commitd6f01bd571753dc2e9628418f94f66139438bcb3 (patch)
tree1ed5492d8abdeb9231d498e9ecf349c7cc1ec3d8 /src/libs/intertrait/cast/arc.rs
parent080cc42bb1da09059dbc35049a7ded0649961e0c (diff)
refactor: replace arc cast panic with an error
Diffstat (limited to 'src/libs/intertrait/cast/arc.rs')
-rw-r--r--src/libs/intertrait/cast/arc.rs21
1 files changed, 14 insertions, 7 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>())),
}
}
}