Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Jun 26, 2019
1 parent 91a9762 commit 2982a2e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions core/any_error.rs
Expand Up @@ -3,12 +3,12 @@ use std::convert::From;
use std::error::Error;

#[allow(dead_code)]
pub type BoxError = Box<dyn AnyError + Sync>;
pub type BoxError = Box<dyn AnyError>;
#[allow(dead_code)]
pub type DynError<'a> = &'a dyn AnyError;

pub trait AnyError: Error + Any + Send + Sync + 'static {}
impl<T> AnyError for T where T: Error + Any + Send + Sync + Sized + 'static {}
pub trait AnyError: Error + Any + Send + 'static {}
impl<T> AnyError for T where T: Error + Any + Send + Sized + 'static {}

impl<T> From<T> for Box<dyn AnyError>
where
Expand Down Expand Up @@ -46,6 +46,21 @@ pub trait DowncastErrorRef: Sized {
T: AnyError + Sized;
}

impl DowncastErrorRef for Box<dyn AnyError> {
fn downcast_ref<T>(&self) -> Option<&T>
where
T: AnyError + Sized,
{
if Any::type_id(&**self) == TypeId::of::<T>() {
let target = self as *const Self as *const &T;
let target = unsafe { &(*target as &T) };
Some(target)
} else {
None
}
}
}

impl DowncastErrorRef for &dyn AnyError {
fn downcast_ref<T>(&self) -> Option<&T>
where
Expand Down

0 comments on commit 2982a2e

Please sign in to comment.