aboutsummaryrefslogtreecommitdiff
path: root/src/libs/intertrait
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-07-28 20:36:53 +0200
committerHampusM <hampus@hampusmat.com>2022-07-31 12:17:51 +0200
commit545e8efddf217f300b26b930f8345d8573c30ec7 (patch)
tree41314bb8a452e70924aa007f11bea1f6338b3718 /src/libs/intertrait
parentf75a7d58135825c4f9094c1e10f36de4a952f455 (diff)
refactor: add Intertrait cast error
Diffstat (limited to 'src/libs/intertrait')
-rw-r--r--src/libs/intertrait/cast/arc.rs24
-rw-r--r--src/libs/intertrait/cast/box.rs26
-rw-r--r--src/libs/intertrait/cast/error.rs17
-rw-r--r--src/libs/intertrait/cast/mod.rs2
-rw-r--r--src/libs/intertrait/cast/rc.rs24
5 files changed, 75 insertions, 18 deletions
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 <http://opensource.org/licenses/MIT>)
//!
//! 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<T: ?Sized + 'static>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>;
+ /// Casts an `Arc` for this trait into that for type `OtherTrait`.
+ fn cast<OtherTrait: ?Sized + 'static>(
+ self: Arc<Self>,
+ ) -> error_stack::Result<Arc<OtherTrait>, CastError>;
}
/// A blanket implementation of `CastArc` for traits extending `CastFrom`, `Sync`, and `Send`.
-impl<S: ?Sized + CastFromSync> CastArc for S
+impl<CastFromSelf: ?Sized + CastFromSync> CastArc for CastFromSelf
{
- fn cast<T: ?Sized + 'static>(self: Arc<Self>) -> Result<Arc<T>, Arc<Self>>
+ fn cast<OtherTrait: ?Sized + 'static>(
+ self: Arc<Self>,
+ ) -> error_stack::Result<Arc<OtherTrait>, CastError>
{
- match caster::<T>((*self).type_id()) {
+ match caster::<OtherTrait>((*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::<CastFromSelf>(),
+ type_name::<OtherTrait>()
+ ))),
}
}
}
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 <http://opensource.org/licenses/MIT>)
//!
//! 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<T: ?Sized + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>>;
+ /// Casts a box to this trait into that of type `OtherTrait`.
+ fn cast<OtherTrait: ?Sized + 'static>(
+ self: Box<Self>,
+ ) -> error_stack::Result<Box<OtherTrait>, CastError>;
}
/// A blanket implementation of `CastBox` for traits extending `CastFrom`.
-impl<S: ?Sized + CastFrom> CastBox for S
+impl<CastFromSelf: ?Sized + CastFrom> CastBox for CastFromSelf
{
- fn cast<T: ?Sized + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>>
+ fn cast<OtherTrait: ?Sized + 'static>(
+ self: Box<Self>,
+ ) -> error_stack::Result<Box<OtherTrait>, CastError>
{
- match caster::<T>((*self).type_id()) {
+ match caster::<OtherTrait>((*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::<CastFromSelf>(),
+ type_name::<OtherTrait>()
+ ))),
}
}
}
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 <http://opensource.org/licenses/MIT>)
//!
//! 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 <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>()
+ ))),
}
}
}