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 +++----- src/libs/intertrait/mod.rs | 77 ++++++++++++++++++--------------------- 5 files changed, 57 insertions(+), 74 deletions(-) (limited to 'src/libs') 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::(), }) } } diff --git a/src/libs/intertrait/mod.rs b/src/libs/intertrait/mod.rs index 4a9322f..d2ace67 100644 --- a/src/libs/intertrait/mod.rs +++ b/src/libs/intertrait/mod.rs @@ -33,18 +33,14 @@ pub mod cast; pub type BoxedCaster = Box; -/// A distributed slice gathering constructor functions for [`Caster`]s. +/// A distributed slice gathering constructor functions for [`Caster`]s. /// /// A constructor function returns `TypeId` of a concrete type involved in the casting -/// and a `Box` of a trait object backed by a [`Caster`]. -/// -/// [`Caster`]: ./struct.Caster.html +/// and a `Box` of a type or trait backed by a [`Caster`]. #[distributed_slice] pub static CASTERS: [fn() -> (TypeId, BoxedCaster)] = [..]; -/// A `HashMap` mapping `TypeId` of a [`Caster`] to an instance of it. -/// -/// [`Caster`]: ./struct.Caster.html +/// A `HashMap` mapping `TypeId` of a [`Caster`] to an instance of it. static CASTER_MAP: Lazy> = Lazy::new(|| { CASTERS .iter() @@ -56,37 +52,36 @@ static CASTER_MAP: Lazy> = Lazy::new(|| .collect() }); -type CastBoxFn = fn(from: Box) -> Result, CasterError>; +type CastBoxFn = fn(from: Box) -> Result, CasterError>; -type CastRcFn = fn(from: Rc) -> Result, CasterError>; +type CastRcFn = fn(from: Rc) -> Result, CasterError>; -type CastArcFn = - fn(from: Arc) -> Result, CasterError>; +type CastArcFn = + fn(from: Arc) -> Result, CasterError>; -/// A `Caster` knows how to cast a reference to or `Box` of a trait object for `Any` -/// to a trait object of trait `Trait`. Each `Caster` instance is specific to a concrete -/// type. That is, it knows how to cast to single specific trait implemented by single -/// specific type. -pub struct Caster +/// A `Caster` knows how to cast a type or trait to the type or trait `Dest`. Each +/// `Caster` instance is specific to a concrete type. That is, it knows how to cast to +/// single specific type or trait implemented by single specific type. +pub struct Caster { - /// Casts a `Box` holding a trait object for `Any` to another `Box` holding a trait - /// object for trait `Trait`. - pub cast_box: CastBoxFn, + /// Casts a `Box` holding a type or trait object for `Any` to another `Box` holding a + /// type or trait `Dest`. + pub cast_box: CastBoxFn, - /// Casts an `Rc` holding a trait object for `Any` to another `Rc` holding a trait - /// object for trait `Trait`. - pub cast_rc: CastRcFn, + /// Casts an `Rc` holding a type or trait for `Any` to another `Rc` holding a type or + /// trait `Dest`. + pub cast_rc: CastRcFn, - /// Casts an `Arc` holding a trait object for `Any + Sync + Send + 'static` - /// to another `Arc` holding a trait object for trait `Trait`. - pub opt_cast_arc: Option>, + /// Casts an `Arc` holding a type or trait for `Any + Sync + Send + 'static` to + /// another `Arc` holding a type or trait for `Dest`. + pub opt_cast_arc: Option>, } -impl Caster +impl Caster { - pub fn new(cast_box: CastBoxFn, cast_rc: CastRcFn) -> Caster + pub fn new(cast_box: CastBoxFn, cast_rc: CastRcFn) -> Caster { - Caster:: { + Caster:: { cast_box, cast_rc, opt_cast_arc: None, @@ -95,12 +90,12 @@ impl Caster #[allow(clippy::similar_names)] pub fn new_sync( - cast_box: CastBoxFn, - cast_rc: CastRcFn, - cast_arc: CastArcFn, - ) -> Caster + cast_box: CastBoxFn, + cast_rc: CastRcFn, + cast_arc: CastArcFn, + ) -> Caster { - Caster:: { + Caster:: { cast_box, cast_rc, opt_cast_arc: Some(cast_arc), @@ -121,18 +116,18 @@ pub enum CasterError CastArcFailed, } -/// Returns a `Caster` from a concrete type `S` to a trait `Trait` implemented -/// by it. -fn get_caster( +/// Returns a `Caster` from a concrete type with the id `type_id` to a type or trait +/// `Dest`. +fn get_caster( type_id: TypeId, -) -> Result<&'static Caster, GetCasterError> +) -> Result<&'static Caster, GetCasterError> { let any_caster = CASTER_MAP - .get(&(type_id, TypeId::of::>())) + .get(&(type_id, TypeId::of::>())) .ok_or(GetCasterError::NotFound)?; any_caster - .downcast_ref::>() + .downcast_ref::>() .ok_or(GetCasterError::DowncastFailed) } @@ -186,7 +181,7 @@ pub trait CastFromSync: CastFrom + Sync + Send + 'static fn arc_any(self: Arc) -> Arc; } -impl CastFrom for Trait +impl CastFrom for Source { fn box_any(self: Box) -> Box { @@ -212,7 +207,7 @@ impl CastFrom for dyn Any + 'static } } -impl CastFromSync for Trait +impl CastFromSync for Source { fn arc_any(self: Arc) -> Arc { -- cgit v1.2.3-18-g5258