[−][src]Crate cex
Checked EXceptions for Rust.
Features
-
Use
Result!( Type throws A,B,.. )
,ret!()
,throw!()
to simulate checked exceptions in Rust -
#[ty_pat] match
for "type as pattern matching" in match expressions. -
Optional backtrace support.
Examples
use cex::*; // accepts even numbers; rejects odd ones and report an error `String` #[cex] fn check_even( a: u32 ) -> Result!( u32 throws String ) { if a % 2 == 1 { throw!( format!( "odd numbers not allowed: a == {}", a )); } else { ret!( a ); } } // accepts non-zero numbers; rejects zeros and report an error of `&'static str` #[cex] fn check_nonzero( b: u32 ) -> Result!( u32 throws &'static str ) { if b == 0 { throw!( "zero not allowed: b == 0" ); } else { ret!( b ) } } struct Underflow; #[cex] fn sub( a: u32, b: u32 ) -> Result!( u32 throws String, &'static str, Underflow ) { let a = check_even( a )?; let b = check_nonzero( b )?; ret!( a+b ); } #[cex] fn distance( a: u32, b: u32 ) -> Result!( u32 throws String, &'static str ) { ret!( sub(a,b).or_else( |err| {#[ty_pat] match err { Underflow => ret!( b-a ), String(s) => throw!( s ), TyPat::<&'static str>(s) => throw!( s ), }})) } #[cex] fn distance2( a: u32, b: u32 ) -> Result!( u32 throws String, &'static str ) { ret!( sub(a,b).or_else( |err| #[ty_pat(gen_throws)] match err { Underflow => ret!( b-a ), })) } #[cex] fn distance3( a: u32, b: u32 ) -> Result!( u32 throws String, &'static str ) { ret!( sub(a,b).or_else( |err| #[ty_pat(gen &'static str, String )] match err { Underflow => ret!( b-a ), })) }
Re-exports
pub use result::*; |
pub use self::log::*; |
Modules
log | Backtrace support |
result |
Macros
Enum | |
frame | A macro to generate a |
Enums
Enum0 | Never type |
Enum1 | |
Enum2 | |
Enum3 | |
Enum4 | |
Enum5 | |
Enum6 | |
Enum7 | |
Enum8 | |
Enum9 | |
Enum10 | |
Enum11 | |
Enum12 | |
Enum13 | |
Enum14 | |
Enum15 | |
Enum16 |
Traits
EnumX | Indicates the prototype for a user-defined exchangeable enum. |
EnumxFrom | Constructs an exchangeable enum from one of its variant type, or from another enum. |
Error | Enum exchange to wrap an |
ExchangeFrom | Constructs an exchangeable enum from another one. |
ExchangeInto | Converts an exchangeable enum into another one. |
FromVariant | Constructs an enum from one of its variant type. |
IntoEnum | Converts an exchangeable enum into another one. |
IntoEnumx | Converts a variant, or an exchangeable enum into another exchangeable enum. |
MapError | Enum exchange for |
Type Definitions
TyPat | Wrapper for non-path types in type pattern matching using #[ty_pat] match |
Attribute Macros
cex | tag an |
Derive Macros
EnumX |