Trait error_iter::ErrorIter

source ·
pub trait ErrorIter: Error + Sized + 'static {
    // Provided method
    fn sources(&self) -> ErrorIterator<'_>  { ... }
}
Expand description

Implement this trait on your error types for free iterators over their sources!

The default implementation provides iterators for any type that implements std::error::Error.

Provided Methods§

source

fn sources(&self) -> ErrorIterator<'_>

Create an iterator over the error and its recursive sources.

use error_iter::ErrorIter as _;
use thiserror::Error;

#[derive(Debug, Error)]
enum Error {
    #[error("Nested error: {0}")]
    Nested(#[source] Box<Error>),

    #[error("Leaf error")]
    Leaf,
}

let error = Error::Nested(Box::new(Error::Leaf));

let mut iter = error.sources();

assert_eq!("Nested error: Leaf error".to_string(), iter.next().unwrap().to_string());
assert_eq!("Leaf error".to_string(), iter.next().unwrap().to_string());
assert!(iter.next().is_none());
assert!(iter.next().is_none());

Implementors§

source§

impl<T> ErrorIter for Twhere T: Error + Sized + 'static,