pub trait WarnExt<E: Error>: Warn<E> + WarnExt<E> {
fn adapt(&mut self) -> Adapt<'_, E, Self>
where
Self: Sized,
{ ... }
fn adapt_map<D, F>(&mut self, func: F) -> AdaptMap<'_, D, E, F, Self>
where
Self: Sized,
D: Error,
F: FnMut(D) -> E,
{ ... }
}
Expand description
Provided Methods
Wraps the sink into the adapter, which is able to convert from different error
types via From
trait
This is especially useful when you try to pass the sink into a subfunction, which yields different errors. See the example below.
Example
use thiserror::Error;
use wurm::prelude::*;
use wurm::CollectAll;
// First error type
#[derive(Debug, Error, PartialEq, Eq)]
#[error("first error")]
struct FooError;
// Second error type, which is converible from `FooError`
#[derive(Debug, Error, PartialEq, Eq)]
#[error("second error: {0}")]
struct BarError(#[from] FooError);
// This function yields errors of type `FooError`
fn foo(warn: &mut impl Warn<FooError>) {
warn.warn(FooError);
}
// This function yields errors of type `FooError`
// So, we need the adapter to call `foo()`
fn bar(warn: &mut impl Warn<BarError>) {
foo(&mut warn.adapt());
}
let mut warn = CollectAll::default();
bar(&mut warn);
// We get exactly one error of type `BarError` after calling `bar()`.
// Note that the error is originally coming from `foo()` and is wrapped.
assert_eq!(warn.0.len(), 1);