diff options
Diffstat (limited to 'src/libs/intertrait/cast/rc.rs')
-rw-r--r-- | src/libs/intertrait/cast/rc.rs | 24 |
1 files changed, 18 insertions, 6 deletions
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 <http://opensource.org/licenses/MIT>) //! //! 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<T: ?Sized + 'static>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>>; + /// Casts an `Rc` for this trait into that for type `OtherTrait`. + fn cast<OtherTrait: ?Sized + 'static>( + self: Rc<Self>, + ) -> error_stack::Result<Rc<OtherTrait>, CastError>; } /// A blanket implementation of `CastRc` for traits extending `CastFrom`. -impl<S: ?Sized + CastFrom> CastRc for S +impl<CastFromSelf: ?Sized + CastFrom> CastRc for CastFromSelf { - fn cast<T: ?Sized + 'static>(self: Rc<Self>) -> Result<Rc<T>, Rc<Self>> + fn cast<OtherTrait: ?Sized + 'static>( + self: Rc<Self>, + ) -> error_stack::Result<Rc<OtherTrait>, CastError> { - match caster::<T>((*self).type_id()) { + match caster::<OtherTrait>((*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::<CastFromSelf>(), + type_name::<OtherTrait>() + ))), } } } |