resext-macro
Procedural macro for ResExt
This crate provides the #[resext] attribute macro for ergonomic error handling. It is not meant to be used directly - use the resext crate instead.
Overview
The proc macro generates all necessary error handling code from a simple attribute:
This expands to approximately 200 lines of boilerplate including:
DisplayandErrortrait implementations- Wrapper struct with inline zero-alloc context storage
- Trait with context method
Fromimplementations for automatic conversion- Type alias for
Result<T, ResErr>
Attribute Options
Basic Options
prefix- Prepended to all error messagessuffix- Appended to all error messages
Context Formatting
msg_prefix- Prepended to each context messagemsg_suffix- Appended to each context messagedelimiter- Separator between contexts (default: " - ")
Source Formatting
source_prefix- Prepended to underlying error (default: "Error: ")
Variant Display
With include_variant = true, errors display as:
Context
Error: Io: No such file or directory
Without it (default):
Context
Error: No such file or directory
Custom Type Alias
// Generated:
pub type AppResult<T> = ;
Context storage inline buffer size
buf_sizesets the size for the inline context storage byte buffer, so with the attribute above, you get 72 bytes of context messages (including delimiters, so it's more accuratelybuf_size - (number_of_messages - 1) * len_of_delimiter)
Heap support
allocadds heap spilling if context exceedsbuf_size
Supported Variant Types
Tuple Variants (Single Field)
Named Fields (Single Field)
Unit Variants
Displays as the variant name: NotFound
Limitations
- Variants must have at most one field
- Multi-field variants are not supported
- Generic enums are not supported
Performance Characteristics
- Context storage uses
[u8; BUF_SIZE]for string data - Zero-alloc even on errors
- Only allocates if alloc attribute is true which is optional
Implementation Details
- The macro uses:
synfor parsing Rust syntaxquotefor code generationproc-macro2for token manipulation
Error Messages
The macro provides helpful error messages:
error: enum variants used in `#[resext]` can only have 1 field
--> src/main.rs:X:Y
|
X | Multi(std::io::Error, String),
| ^^^^^^^^^^^^^^^^^^^^^^^^
License
MIT - See LICENSE for details.
See Also
- resext - Main crate documentation
- ResExt repository