bare_err_tree

Struct ErrTree

Source
pub struct ErrTree<'a> { /* private fields */ }
Expand description

Intermediate struct for printing created by AsErrTree.

Only allowing construction through Self::with_pkg and Self::no_pkg allows arbitrary combinations of metadata tracking without changing construction syntax. Sources are stored under three layers of indirection to allow for maximum type and size flexibility without generics or heap allocation.

See tree to reduce Self::with_pkg boilerplate.

§Manual Implementation Example

use bare_err_tree::{ErrTree, ErrTreePkg, AsErrTree, ErrTreeConv};

#[derive(Debug)]
pub struct HighLevelIo {
    source: std::io::Error,
    _pkg: ErrTreePkg,
}

impl HighLevelIo {
    #[track_caller]
    pub fn new(source: std::io::Error) -> Self {
        Self {
            source,
            _pkg: ErrTreePkg::new(),
        }
    }
}

impl AsErrTree for HighLevelIo {
    fn as_err_tree(&self, func: &mut dyn FnMut(ErrTree<'_>)) {
        // Cast to ErrTree adapter via Error
        let source = ErrTreeConv::from(&self.source as &dyn Error);
        // Convert to a single item iterator
        let source_iter = &mut core::iter::once(source);

        // Call the formatting function
        (func)(ErrTree::with_pkg(self, source_iter, &self._pkg));
    }
}

impl Error for HighLevelIo {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(&self.source)
    }
}
impl Display for HighLevelIo {
    ...
}

Implementations§

Source§

impl<'a> ErrTree<'a>

Source

pub fn with_pkg( inner: &'a dyn Error, sources: &'a mut dyn Iterator<Item = ErrTreeConv<'a>>, pkg: &'a ErrTreePkg, ) -> Self

Common constructor, with metadata.

Source

pub fn no_pkg( inner: &'a dyn Error, sources: &'a mut dyn Iterator<Item = ErrTreeConv<'a>>, ) -> Self

Constructor for when metadata needs to be hidden.

Source

pub fn sources(self) -> impl Iterator<Item = ErrTreeConv<'a>>

Consumes this tree to return its sources

Auto Trait Implementations§

§

impl<'a> Freeze for ErrTree<'a>

§

impl<'a> !RefUnwindSafe for ErrTree<'a>

§

impl<'a> !Send for ErrTree<'a>

§

impl<'a> !Sync for ErrTree<'a>

§

impl<'a> Unpin for ErrTree<'a>

§

impl<'a> !UnwindSafe for ErrTree<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.