Purpose
This is a proof of concept of project aiming at implementing "checked exception" like error handling in Rust via proc-macro.
It is based on enumx
crate. See ../enumx/README.md
for more.
Naming
It provides CeX
structures, that is read as "Concrete Error Exchange" or "Checked EXception", at your will.
A CeX
is essentially a composition of EnumX
for exchange, and a Backtrace
for stack backtracing.
CeX
es composed of x errors are named as Ce1
, Ce2
, ... etc.
type converting rules:
-
Each error type in a
CeX
is convertable to thisCeX
. -
An
CeX
T is convertable to anotherCeX
U if all errors in T are in U.
Inside a function with #[cex]
attribute and returning Result<_,CeX<_,...>
, the code can return or try(?
) any type that is convertable to the CeX
type in the Err
part of the function result type.
However, explicit ?
or return
are required.
If you do not want backtracing, just use Result<_,EnumX<_,...>
instead.
Examples
-
A function checking 2 errors.
assert_eq!; assert_eq!; assert_eq!;
-
Some 2-errors
CeX
that is convertable to 3-errorsCeX
.assert_eq!; assert_eq!; assert_eq!; assert_eq!;
-
Errors' order does not matter.
assert_eq!; assert_eq!; assert_eq!;
-
Those who are not interested in "checked exceptions" can work with the libs that using "checked exceptions".
assert_eq!; assert_eq!; assert_eq!; assert_eq!;
-
Backtracing support.
if let Err = foo
The code listed above will print something like:
Backtrace( [ ThrowPoint { line: 110, column: 5, function: "foo", module: "cex_test", file: "cex_test/src/lib.rs" }, ThrowPoint { line: 115, column: 5, function: "bar", module: "cex_test", file: "cex_test/src/lib.rs" } ] )
Limitation
-
The library users should use concrete error types as the type parameters of CeX, rather than generics.
This restriction may be removed in the upcoming version 0.2.0.
-
DO NOT use nested
CeX
and expect it be flatterned. For example,Ce2<i32,usize>
cannot be converted toCe2<Ce2<i32,String>,usize>
in#[cex] fn
.Currently this project does nothing to support flatterning.
-
Current version supports up to 32 variants in a
CeX
.In otherwords,
Ce1
..=Ce32
is available butCe33
but the succeeding enums are not.If it is proved that more than comination of 32 errors are useful in practice, they may be supported in later version.
Notice that the compile time will grow in
O( n*n )
impl
s. -
Some sort of identifiers are reserved for implementation usage.
cex
/cex_derive
crates reserve identifiers starting with__CeX
,__cex
and all thatenumx
/enum_derive
reserves.
License
Licensed under MIT.