From e665c2a3ca9b15f406c8e12e4a7ab372fcad4d36 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 3 Nov 2022 21:15:24 +0100 Subject: refactor: improve readability of cast functions --- src/libs/intertrait/cast/arc.rs | 23 ++++++++++------------- src/libs/intertrait/cast/box.rs | 10 +++++----- src/libs/intertrait/cast/rc.rs | 10 +++++----- 3 files changed, 20 insertions(+), 23 deletions(-) (limited to 'src/libs/intertrait') diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs index 33d84d2..1742c32 100644 --- a/src/libs/intertrait/cast/arc.rs +++ b/src/libs/intertrait/cast/arc.rs @@ -31,19 +31,16 @@ impl CastArc for CastFromSelf self: Arc, ) -> Result, CastError> { - let caster = get_caster::((*self).type_id()).map_or_else( - || { - Err(CastError::CastFailed { - from: type_name::(), - to: type_name::(), - }) - }, - Ok, - )?; + let caster = + get_caster::((*self).type_id()).ok_or(CastError::CastFailed { + from: type_name::(), + to: type_name::(), + })?; - match caster.opt_cast_arc { - Some(cast_arc) => Ok(cast_arc(self.arc_any())), - None => Err(CastError::NotArcCastable(type_name::())), - } + let cast_arc = caster + .opt_cast_arc + .ok_or(CastError::NotArcCastable(type_name::()))?; + + Ok(cast_arc(self.arc_any())) } } diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs index c463c2f..5694d97 100644 --- a/src/libs/intertrait/cast/box.rs +++ b/src/libs/intertrait/cast/box.rs @@ -30,12 +30,12 @@ impl CastBox for CastFromSelf self: Box, ) -> Result, CastError> { - match get_caster::((*self).type_id()) { - Some(caster) => Ok((caster.cast_box)(self.box_any())), - None => Err(CastError::CastFailed { + let caster = + get_caster::((*self).type_id()).ok_or(CastError::CastFailed { from: type_name::(), to: type_name::(), - }), - } + })?; + + Ok((caster.cast_box)(self.box_any())) } } diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs index 63c0024..805bcd7 100644 --- a/src/libs/intertrait/cast/rc.rs +++ b/src/libs/intertrait/cast/rc.rs @@ -30,12 +30,12 @@ impl CastRc for CastFromSelf self: Rc, ) -> Result, CastError> { - match get_caster::((*self).type_id()) { - Some(caster) => Ok((caster.cast_rc)(self.rc_any())), - None => Err(CastError::CastFailed { + let caster = + get_caster::((*self).type_id()).ok_or(CastError::CastFailed { from: type_name::(), to: type_name::(), - }), - } + })?; + + Ok((caster.cast_rc)(self.rc_any())) } } -- cgit v1.2.3-18-g5258