pub trait ResultExt<T, E> {
// Required methods
fn ctx<C: IntoErrorContext>(
self,
msg: C,
) -> Result<T, Box<ComposableError<E>>>;
fn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
where F: FnOnce() -> String;
}Expand description
Extension trait for adding context to Result types ergonomically.
This trait provides a more natural API for error context compared to
manual .map_err() chains, reducing boilerplate while maintaining
full type safety.
§Performance
The ctx_with method uses lazy evaluation,
meaning the closure is only executed when an error actually occurs.
This provides the same 2.1x performance benefit as the context! macro.
§Examples
§Basic Usage
use error_rail::traits::ResultExt;
use error_rail::ComposableError;
fn read_file() -> Result<String, Box<ComposableError<std::io::Error>>> {
std::fs::read_to_string("data.txt")
.ctx("reading data file")
}§Lazy Context (Recommended for Performance)
use error_rail::traits::ResultExt;
use error_rail::ComposableError;
fn process(user_id: u64) -> Result<(), Box<ComposableError<&'static str>>> {
let result: Result<(), &str> = Err("not found");
result.ctx_with(|| format!("processing user {}", user_id))
}§Chaining Multiple Contexts
use error_rail::traits::ResultExt;
use error_rail::ComposableError;
fn complex_operation() -> Result<String, Box<ComposableError<std::io::Error>>> {
std::fs::read_to_string("config.toml")
.ctx("loading config")
.map(|s| s.to_uppercase())
}Required Methods§
Sourcefn ctx<C: IntoErrorContext>(self, msg: C) -> Result<T, Box<ComposableError<E>>>
fn ctx<C: IntoErrorContext>(self, msg: C) -> Result<T, Box<ComposableError<E>>>
Adds a static context message to the error.
This method wraps the error in a ComposableError with the given
context message, then boxes it for ergonomic return types.
§Arguments
msg- A static string describing what operation was being performed.
§Examples
use error_rail::traits::ResultExt;
let result: Result<(), &str> = Err("failed");
let with_context = result.ctx("performing operation");
assert!(with_context.is_err());Sourcefn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
fn ctx_with<F>(self, f: F) -> Result<T, Box<ComposableError<E>>>
Adds a lazily-evaluated context message to the error.
The closure is only called if the Result is an Err, providing
optimal performance on success paths (2.1x faster than eager evaluation).
§Arguments
f- A closure that produces the context message.
§Examples
use error_rail::traits::ResultExt;
let user_id = 42;
let result: Result<(), &str> = Err("not found");
let with_context = result.ctx_with(|| format!("user_id: {}", user_id));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.