pub trait IntoBoxedError {
type Output;
// Required method
fn into_boxed_error(self) -> Self::Output;
}Expand description
A trait for converting rootcause Reports into boxed error trait objects.
This trait provides the .into_boxed_error() method for converting
rootcause reports into standard Rust error trait objects. It’s implemented
for both Report and Result<T, Report>, making it easy to integrate
with APIs that expect Box<dyn Error>.
The specific type of boxed error depends on the thread safety marker:
§Examples
§Converting a Result with SendSync Report
use std::error::Error;
use rootcause::{compat::boxed_error::IntoBoxedError, prelude::*};
fn uses_rootcause() -> Result<i32, Report> {
Err(report!("failed"))
}
fn uses_boxed_error() -> Result<i32, Box<dyn Error + Send + Sync>> {
let value = uses_rootcause().into_boxed_error()?;
Ok(value)
}§Converting a Local Report
use std::{error::Error, rc::Rc};
use rootcause::{compat::boxed_error::IntoBoxedError, markers::Local, prelude::*};
let local_report: Report<_, _, Local> = report!("error").into_local().attach(Rc::new("data"));
let boxed_err: Box<dyn Error> = local_report.into_boxed_error();
// The boxed error displays the full report structure
println!("{}", boxed_err);§Using From Instead
You can also use the From trait for explicit conversions:
use std::error::Error;
use rootcause::prelude::*;
let report: Report = report!("error");
let boxed_err: Box<dyn Error + Send + Sync> = report.into();Required Associated Types§
Sourcetype Output
type Output
The type produced by the conversion.
- For
Report<_, _, SendSync>: producesBox<dyn Error + Send + Sync> - For
Report<_, _, Local>: producesBox<dyn Error> - For
Result<T, Report<_, _, SendSync>>: producesResult<T, Box<dyn Error + Send + Sync>> - For
Result<T, Report<_, _, Local>>: producesResult<T, Box<dyn Error>>
Required Methods§
Sourcefn into_boxed_error(self) -> Self::Output
fn into_boxed_error(self) -> Self::Output
Converts this value into a boxed error type.
For Report, this wraps the report in a boxed error trait object. For
Result<T, Report>, this converts the error variant while preserving
the success value.
The thread safety of the resulting boxed error matches the thread safety of the original report.
§Examples
use std::error::Error;
use rootcause::{compat::boxed_error::IntoBoxedError, prelude::*};
// Convert a result
let result: Result<i32, Report> = Ok(42);
let converted: Result<i32, Box<dyn Error + Send + Sync>> = result.into_boxed_error();
assert_eq!(converted.unwrap(), 42);
// Convert a report
let report: Report = report!("failed");
let boxed_err: Box<dyn Error + Send + Sync> = report.into_boxed_error();