Skip to main content

IntoBoxedError

Trait IntoBoxedError 

Source
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:

  • SendSync reports convert to Box<dyn Error + Send + Sync>
  • Local reports convert to Box<dyn Error>

§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§

Source

type Output

The type produced by the conversion.

Required Methods§

Source

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();

Implementations on Foreign Types§

Source§

impl<T, C: ?Sized, O> IntoBoxedError for Result<T, Report<C, O, Local>>

Source§

impl<T, C: ?Sized, O> IntoBoxedError for Result<T, Report<C, O, SendSync>>

Source§

type Output = Result<T, Box<dyn Error + Sync + Send>>

Source§

fn into_boxed_error(self) -> Self::Output

Implementors§

Source§

impl<C: ?Sized, O> IntoBoxedError for Report<C, O, Local>

Source§

impl<C: ?Sized, O> IntoBoxedError for Report<C, O, SendSync>

Source§

type Output = Box<dyn Error + Sync + Send>