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 ++++++++++++++++++------ src/libs/intertrait/cast/box.rs | 26 ++++++++++++++++++++------ src/libs/intertrait/cast/error.rs | 17 +++++++++++++++++ src/libs/intertrait/cast/mod.rs | 2 ++ src/libs/intertrait/cast/rc.rs | 24 ++++++++++++++++++------ 5 files changed, 75 insertions(+), 18 deletions(-) create mode 100644 src/libs/intertrait/cast/error.rs (limited to 'src/libs') 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::() + ))), } } } diff --git a/src/libs/intertrait/cast/box.rs b/src/libs/intertrait/cast/box.rs index c88fb84..11f631a 100644 --- a/src/libs/intertrait/cast/box.rs +++ b/src/libs/intertrait/cast/box.rs @@ -9,22 +9,36 @@ //! MIT license (LICENSE-MIT or ) //! //! at your option. + +use std::any::type_name; + +use error_stack::report; + +use crate::libs::intertrait::cast::error::CastError; use crate::libs::intertrait::{caster, CastFrom}; pub trait CastBox { - /// Casts a box to this trait into that of type `T`. If fails, returns the receiver. - fn cast(self: Box) -> Result, Box>; + /// Casts a box to this trait into that of type `OtherTrait`. + fn cast( + self: Box, + ) -> error_stack::Result, CastError>; } /// A blanket implementation of `CastBox` for traits extending `CastFrom`. -impl CastBox for S +impl CastBox for CastFromSelf { - fn cast(self: Box) -> Result, Box> + fn cast( + self: Box, + ) -> error_stack::Result, CastError> { - match caster::((*self).type_id()) { + match caster::((*self).type_id()) { Some(caster) => Ok((caster.cast_box)(self.box_any())), - None => Err(self), + None => Err(report!(CastError).attach_printable(format!( + "From {} to {}", + type_name::(), + type_name::() + ))), } } } diff --git a/src/libs/intertrait/cast/error.rs b/src/libs/intertrait/cast/error.rs new file mode 100644 index 0000000..e4211b1 --- /dev/null +++ b/src/libs/intertrait/cast/error.rs @@ -0,0 +1,17 @@ +use std::fmt; +use std::fmt::{Display, Formatter}; + +use error_stack::Context; + +#[derive(Debug)] +pub struct CastError; + +impl Display for CastError +{ + fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result + { + fmt.write_str("Failed to cast between traits") + } +} + +impl Context for CastError {} diff --git a/src/libs/intertrait/cast/mod.rs b/src/libs/intertrait/cast/mod.rs index 83f7210..a0342cf 100644 --- a/src/libs/intertrait/cast/mod.rs +++ b/src/libs/intertrait/cast/mod.rs @@ -9,6 +9,8 @@ //! MIT license (LICENSE-MIT or ) //! //! at your option. +pub mod error; + mod arc; mod r#box; mod rc; diff --git a/src/libs/intertrait/cast/rc.rs b/src/libs/intertrait/cast/rc.rs index b53ced0..6cd377e 100644 --- a/src/libs/intertrait/cast/rc.rs +++ b/src/libs/intertrait/cast/rc.rs @@ -9,24 +9,36 @@ //! MIT license (LICENSE-MIT or ) //! //! at your option. +use std::any::type_name; use std::rc::Rc; +use error_stack::report; + +use crate::libs::intertrait::cast::error::CastError; use crate::libs::intertrait::{caster, CastFrom}; pub trait CastRc { - /// Casts an `Rc` for this trait into that for type `T`. - fn cast(self: Rc) -> Result, Rc>; + /// Casts an `Rc` for this trait into that for type `OtherTrait`. + fn cast( + self: Rc, + ) -> error_stack::Result, CastError>; } /// A blanket implementation of `CastRc` for traits extending `CastFrom`. -impl CastRc for S +impl CastRc for CastFromSelf { - fn cast(self: Rc) -> Result, Rc> + fn cast( + self: Rc, + ) -> error_stack::Result, CastError> { - match caster::((*self).type_id()) { + match caster::((*self).type_id()) { Some(caster) => Ok((caster.cast_rc)(self.rc_any())), - None => Err(self), + None => Err(report!(CastError).attach_printable(format!( + "From {} to {}", + type_name::(), + type_name::() + ))), } } } -- cgit v1.2.3-18-g5258