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/mod.rs | 77 ++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 41 deletions(-) (limited to 'src/libs/intertrait/mod.rs') 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