From c06670e72bdef284c2d206079683b7d1daf0ed66 Mon Sep 17 00:00:00 2001 From: Jessie Date: Fri, 22 Sep 2023 03:25:30 -0400 Subject: [PATCH] fix Boxed types --- src/autotraits.rs | 11 +++++++++++ src/lib.rs | 11 ++++------- tests/test.rs | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 src/autotraits.rs diff --git a/src/autotraits.rs b/src/autotraits.rs new file mode 100644 index 0000000..110832b --- /dev/null +++ b/src/autotraits.rs @@ -0,0 +1,11 @@ +#![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 d158245..a703632 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,15 +8,14 @@ use alloc::format; use alloc::string::String; use core::fmt::{Debug, Display, Formatter}; use core::any::Any; +use crate::autotraits::{NotAnything, NotNothing}; +mod autotraits; // ============================== // Anything // ============================== -auto trait NotAnything {} -impl !NotAnything for Anything {} - pub struct Anything { error: Box, @@ -118,12 +117,11 @@ impl DynError for T { } } + // ============================== -// Anything +// Nothing // ============================== -auto trait NotNothing {} -impl !NotNothing for Nothing {} #[derive(Debug)] pub struct Nothing; @@ -131,4 +129,3 @@ pub struct Nothing; impl From for Nothing { fn from(_: T) -> Self { Nothing } } - diff --git a/tests/test.rs b/tests/test.rs index 6d72f5c..ddf4bb6 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -15,10 +15,37 @@ pub fn func_3() -> Result<(),Nothing> { Ok(()) } +fn func_4() -> Anything { + let a= std::io::Error::last_os_error(); + Anything::from(a) +} + +fn func_5() -> Result<(), Anything> { + fn func_5a() -> Result<(), Anything> { + fn func_5b() -> Result<(), Anything> { + fn func_5c() -> Result<(), Anything> { + fn func_5d() -> Result<(), Anything> { + Err("Hello World")?; + Ok(()) + } + func_5d()?; + Ok(()) + } + func_5c()?; + Ok(()) + } + func_5b()?; + Ok(()) + } + func_5a()?; + Ok(()) +} #[test] pub fn test() { - println!("{:?}", func_1().unwrap_err()); - println!("{:?}", func_2().unwrap_err()); - println!("{:?}", func_3().unwrap_err()); + println!("func1: {:?}\n", func_1().unwrap_err()); + println!("func2: {:?}\n", func_2().unwrap_err()); + println!("func3: {:?}\n", func_3().unwrap_err()); + println!("func4: {:?}\n", func_4()); + println!("func5: {:?}\n", func_5().unwrap_err()); } \ No newline at end of file