diff --git a/src/anything.rs b/src/anything.rs new file mode 100644 index 0000000..13c8403 --- /dev/null +++ b/src/anything.rs @@ -0,0 +1,107 @@ +use alloc::boxed::Box; +use alloc::format; +use alloc::string::String; +use core::any::Any; +use core::fmt::{Debug, Display, Formatter}; +use core::panic::Location; + +pub auto trait NotAnything {} +impl !NotAnything for Anything {} +impl NotAnything for Box {} + +pub struct Anything { + error: Box, + + #[cfg(debug_assertions)] + origin: &'static Location<'static>, +} + +impl Anything { + + #[track_caller] + pub fn new(error: T) -> Anything { + Anything { + error: Box::new(error), + origin: Location::caller(), + } + } + + #[track_caller] + pub fn new_error(error: T) -> Result<(), Anything> { + Err(Anything { + error: Box::new(error), + origin: Location::caller(), + }) + } + + #[track_caller] + pub fn assert(error_if_false: bool, error: T) -> Result<(), Anything> { + if error_if_false { return Ok(()) } + Err(Anything { + error: Box::new(error), + origin: Location::caller(), + }) + } + + pub fn get_error(&self) -> &dyn Any { + self.error.as_any() + } + + pub fn get_origin(&self) -> Option> { + #[cfg(debug_assertions)] + return Some(self.origin.clone()); + #[cfg(not(debug_assertions))] + return None; + } + +} + +impl Display for Anything { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, + "[{}:{}:{}] {:?}", + self.origin.file(), + self.origin.line(), + self.origin.column(), + self.error.format() + ) + } +} + +impl Debug for Anything { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + write!(f, + "'{:?}'\n at {}:{}:{}", + self.error.format(), + self.origin.file(), + self.origin.line(), + self.origin.column() + ) + } +} + + +impl From for Anything { + #[track_caller] + fn from(value: T) -> Self { + Anything { + error: Box::new(value), + origin: Location::caller(), + } + } +} + +pub trait DynError { + fn as_any(&self) -> &dyn Any; + fn format(&self) -> String; +} + +impl DynError for T { + fn as_any(&self) -> &dyn Any { + self as &dyn Any + } + + fn format(&self) -> String { + format!("{:?}", self) + } +} diff --git a/src/autotraits.rs b/src/autotraits.rs deleted file mode 100644 index 110832b..0000000 --- a/src/autotraits.rs +++ /dev/null @@ -1,11 +0,0 @@ -#![allow(suspicious_auto_trait_impls)] - -pub auto trait NotNothing {} -impl !NotNothing for super::Nothing {} - -pub auto trait NotAnything {} -impl !NotAnything for super::Anything {} - -impl NotNothing for alloc::boxed::Box {} -impl NotAnything for alloc::boxed::Box {} - diff --git a/src/lib.rs b/src/lib.rs index a703632..a6cdfc8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,131 +1,10 @@ -#![no_std] #![feature(negative_impls, auto_traits)] +#![no_std] #![feature(negative_impls, auto_traits, const_type_id)] +#![allow(suspicious_auto_trait_impls)] extern crate alloc; -use core::panic::Location; -use alloc::boxed::Box; -use alloc::format; -use alloc::string::String; -use core::fmt::{Debug, Display, Formatter}; -use core::any::Any; -use crate::autotraits::{NotAnything, NotNothing}; +pub(crate) mod anything; +pub(crate) mod nothing; -mod autotraits; - -// ============================== -// Anything -// ============================== - -pub struct Anything { - error: Box, - - #[cfg(debug_assertions)] - origin: &'static Location<'static>, -} - -impl Anything { - - #[track_caller] - pub fn new(error: T) -> Anything { - Anything { - error: Box::new(error), - origin: Location::caller(), - } - } - - #[track_caller] - pub fn new_error(error: T) -> Result<(), Anything> { - Err(Anything { - error: Box::new(error), - origin: Location::caller(), - }) - } - - #[track_caller] - pub fn assert(error_if_false: bool, error: T) -> Result<(), Anything> { - if error_if_false { return Ok(()) } - Err(Anything { - error: Box::new(error), - origin: Location::caller(), - }) - } - - pub fn get_error(&self) -> &dyn Any { - self.error.as_any() - } - - pub fn get_origin(&self) -> Option> { - #[cfg(debug_assertions)] - return Some(self.origin.clone()); - #[cfg(not(debug_assertions))] - return None; - } - -} - -impl Display for Anything { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, - "[{}:{}:{}] {:?}", - self.origin.file(), - self.origin.line(), - self.origin.column(), - self.error.format() - ) - } -} - -impl Debug for Anything { - fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - write!(f, - "'{:?}'\n at {}:{}:{}", - self.error.format(), - self.origin.file(), - self.origin.line(), - self.origin.column() - ) - } -} - - -impl From for Anything { - #[track_caller] - fn from(value: T) -> Self { - Anything { - error: Box::new(value), - origin: Location::caller(), - } - } -} - -// ============================== -// Error Type -// ============================== - -pub trait DynError { - fn as_any(&self) -> &dyn Any; - fn format(&self) -> String; -} - -impl DynError for T { - fn as_any(&self) -> &dyn Any { - self as &dyn Any - } - - fn format(&self) -> String { - format!("{:?}", self) - } -} - - -// ============================== -// Nothing -// ============================== - - -#[derive(Debug)] -pub struct Nothing; - -impl From for Nothing { - fn from(_: T) -> Self { Nothing } -} +pub use anything::*; +pub use nothing::*; \ No newline at end of file diff --git a/src/nothing.rs b/src/nothing.rs new file mode 100644 index 0000000..d0c1816 --- /dev/null +++ b/src/nothing.rs @@ -0,0 +1,11 @@ + +pub auto trait NotNothing {} +impl !NotNothing for Nothing {} +impl NotNothing for alloc::boxed::Box {} + +#[derive(Debug)] +pub struct Nothing; + +impl From for Nothing { + fn from(_: T) -> Self { Nothing } +} \ No newline at end of file