From 545e8efddf217f300b26b930f8345d8573c30ec7 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 28 Jul 2022 20:36:53 +0200 Subject: refactor: add Intertrait cast error --- src/libs/intertrait/cast/arc.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/libs/intertrait/cast/arc.rs') diff --git a/src/libs/intertrait/cast/arc.rs b/src/libs/intertrait/cast/arc.rs index 4099ae7..f8e0f11 100644 --- a/src/libs/intertrait/cast/arc.rs +++ b/src/libs/intertrait/cast/arc.rs @@ -9,24 +9,36 @@ //! MIT license (LICENSE-MIT or ) //! //! at your option. +use std::any::type_name; use std::sync::Arc; +use error_stack::report; + +use crate::libs::intertrait::cast::error::CastError; use crate::libs::intertrait::{caster, CastFromSync}; pub trait CastArc { - /// Casts an `Arc` for this trait into that for type `T`. - fn cast(self: Arc) -> Result, Arc>; + /// Casts an `Arc` for this trait into that for type `OtherTrait`. + fn cast( + self: Arc, + ) -> error_stack::Result, CastError>; } /// A blanket implementation of `CastArc` for traits extending `CastFrom`, `Sync`, and `Send`. -impl CastArc for S +impl CastArc for CastFromSelf { - fn cast(self: Arc) -> Result, Arc> + fn cast( + self: Arc, + ) -> error_stack::Result, CastError> { - match caster::((*self).type_id()) { + match caster::((*self).type_id()) { Some(caster) => Ok((caster.cast_arc)(self.arc_any())), - None => Err(self), + None => Err(report!(CastError).attach_printable(format!( + "From {} to {}", + type_name::(), + type_name::() + ))), } } } -- cgit v1.2.3-18-g5258