Expand description
Modern, flat, zero-cost error flow based on pure struct errors.
§⚠️ WARNING: UNAUDITED CODE
This crate was AI-generated and has not undergone human review. It likely contains bugs and should NOT be used in production. The API is subject to change after audit.
struct_error inverts the traditional Rust error model:
- Errors are first-class structs, not enum variants.
- No manual
Ok/Errwrapping inside#[throws]functions. - Pattern match by type name without destructuring nested
Result/Enumlayers. - Zero runtime cost: everything is resolved at compile time via procedural macros.
§Core Concepts
| Item | Purpose |
|---|---|
#[error] | Define an atomic error struct with auto-derived Debug, Display, and Error. |
#[united_error] | Create a compile-time alias for a set of errors. |
#[throws] | Rewrite a function to implicitly return Result<T, Unt<...>> and intercept ?. |
match_error! | Blind, type-driven pattern matching on errors. |
throw! | Explicitly throw an error inside a #[throws] function. |
Unt | Runtime heterogeneous list (nested enum) representing the implicit union. |
End | Uninhabited terminator for the Unt HList. |
§Examples
A complete end-to-end example:
use struct_error::{error, united_error, throws, match_error, throw};
#[error("resource not found: {}", self.id)]
pub struct NotFound {
pub id: u64,
}
#[error("connection timed out after {}ms", self.ms)]
pub struct Timeout {
pub ms: u64,
}
#[united_error(NotFound, Timeout)]
pub struct AppError;
#[throws(NotFound, Timeout)]
pub fn fetch_resource(id: u64) -> String {
if id == 0 {
throw!(NotFound { id });
}
if id > 100 {
throw!(Timeout { ms: 5000 });
}
format!("resource-{}", id)
}
#[throws(NotFound, Timeout)]
pub fn process(id: u64) -> String {
let res = fetch_resource(id)?;
res.to_uppercase()
}
fn main() {
let result = process(0);
match_error!(result {
Ok(v) => println!("success: {}", v),
NotFound { id } => println!("not found: {}", id),
Timeout { ms } => println!("timeout: {}ms", ms),
});
}Re-exports§
pub extern crate macro_magic;
Macros§
- __
throws_ impl - Internal function-like proc macro called by
__throws_cpsat the end of the token forwarding chain. - match_
error - Blind, type-driven pattern matching on errors.
- throw
- Explicitly throw an error inside a
#[throws]function.
Enums§
- End
- Uninhabited terminator for the
UntHList. - Unt
- Heterogeneous list (nested enum) — runtime representation of the implicit error union.
Attribute Macros§
- __
struct_ error_ members - Internal attribute macro that encodes the member list on structs generated by
#[united_error]. - __
throws_ cps - Internal attribute macro used as the CPS callback for
macro_magic::forward_tokens!. - error
- Defines an atomic error struct, automatically deriving
Debug,Display, andError. - throws
- Replaces the function’s return type with an implicit error union and rewrites control flow.
- united_
error - Defines a compile-time alias for a set of errors (a united error).