From 4e8951ff740bddb39d9782c18ed85898d0b978fb Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 10 Nov 2022 21:48:55 +0100 Subject: refactor: improve type param names, docs & more of casting --- src/libs/intertrait/cast/arc.rs | 18 +++++++----------- src/libs/intertrait/cast/box.rs | 16 ++++++---------- src/libs/intertrait/cast/error.rs | 4 ++-- src/libs/intertrait/cast/rc.rs | 16 ++++++---------- 4 files changed, 21 insertions(+), 33 deletions(-) (limited to 'src/libs/intertrait/cast') diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs index 6d3867c..a1b2c30 100644 --- a/src/libs/intertrait/cast/arc.rs +++ b/src/libs/intertrait/cast/arc.rs @@ -17,31 +17,27 @@ use crate::libs::intertrait::{get_caster, CastFromSync}; pub trait CastArc { - /// Casts an `Arc` for this trait into that for type `OtherTrait`. - fn cast( - self: Arc, - ) -> Result, CastError>; + /// Casts an `Arc` with `Self` into an `Arc` with `Dest`. + fn cast(self: Arc) -> Result, CastError>; } /// A blanket implementation of `CastArc` for traits extending `CastFrom`, `Sync`, and /// `Send`. impl CastArc for CastFromSelf { - fn cast( - self: Arc, - ) -> Result, CastError> + fn cast(self: Arc) -> Result, CastError> { - let caster = get_caster::((*self).type_id()) - .map_err(CastError::GetCasterFailed)?; + 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_or(CastError::NotArcCastable(type_name::()))?; cast_arc(self.arc_any()).map_err(|err| CastError::CastFailed { source: err, from: type_name::(), - to: type_name::(), + to: type_name::(), }) } } diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs index 0e160f4..fcd5f70 100644 --- a/src/libs/intertrait/cast/box.rs +++ b/src/libs/intertrait/cast/box.rs @@ -17,26 +17,22 @@ use crate::libs::intertrait::{get_caster, CastFrom}; pub trait CastBox { - /// Casts a box to this trait into that of type `OtherTrait`. - fn cast( - self: Box, - ) -> Result, CastError>; + /// Casts a `Box` with `Self` into a `Box` with `Dest`. + fn cast(self: Box) -> Result, CastError>; } /// A blanket implementation of `CastBox` for traits extending `CastFrom`. impl CastBox for CastFromSelf { - fn cast( - self: Box, - ) -> Result, CastError> + fn cast(self: Box) -> Result, CastError> { - let caster = get_caster::((*self).type_id()) - .map_err(CastError::GetCasterFailed)?; + let caster = + get_caster::((*self).type_id()).map_err(CastError::GetCasterFailed)?; (caster.cast_box)(self.box_any()).map_err(|err| CastError::CastFailed { source: err, from: type_name::(), - to: type_name::(), + to: type_name::(), }) } } diff --git a/src/libs/intertrait/cast/error.rs b/src/libs/intertrait/cast/error.rs index e6d86a5..d253fc5 100644 --- a/src/libs/intertrait/cast/error.rs +++ b/src/libs/intertrait/cast/error.rs @@ -6,7 +6,7 @@ pub enum CastError #[error("Failed to get caster")] GetCasterFailed(#[from] GetCasterError), - #[error("Failed to cast from trait {from} to trait {to}")] + #[error("Failed to cast from {from} to {to}")] CastFailed { #[source] @@ -15,6 +15,6 @@ pub enum CastError to: &'static str, }, - #[error("Trait '{0}' can't be cast to Arc")] + #[error("'{0}' can't be cast to an Arc")] NotArcCastable(&'static str), } diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs index 906490d..8567d1e 100644 --- a/src/libs/intertrait/cast/rc.rs +++ b/src/libs/intertrait/cast/rc.rs @@ -17,26 +17,22 @@ use crate::libs::intertrait::{get_caster, CastFrom}; pub trait CastRc { - /// Casts an `Rc` for this trait into that for type `OtherTrait`. - fn cast( - self: Rc, - ) -> Result, CastError>; + /// Casts an `Rc` with `Self `into a `Rc` with `Dest`. + fn cast(self: Rc) -> Result, CastError>; } /// A blanket implementation of `CastRc` for traits extending `CastFrom`. impl CastRc for CastFromSelf { - fn cast( - self: Rc, - ) -> Result, CastError> + fn cast(self: Rc) -> Result, CastError> { - let caster = get_caster::((*self).type_id()) - .map_err(CastError::GetCasterFailed)?; + let caster = + get_caster::((*self).type_id()).map_err(CastError::GetCasterFailed)?; (caster.cast_rc)(self.rc_any()).map_err(|err| CastError::CastFailed { source: err, from: type_name::(), - to: type_name::(), + to: type_name::(), }) } } -- cgit v1.2.3-18-g5258