Skip to main content

impl_from_io_error

Macro impl_from_io_error 

Source
macro_rules! impl_from_io_error {
    ($($ty:ty),+ $(,)?) => { ... };
    ($ty:ty => $variant:ident) => { ... };
}
Expand description

Generates From<std::io::Error> for one or more serializer error enums.

Serializer errors store the IO failure as a string rather than a std::io::Error, because they derive Eq/PartialEq/Clone (the test_write* helpers require E: Eq) and std::io::Error implements none of those. That lossy conversion is why thiserror’s #[from] cannot be used here: it requires the variant field to be the source error type.

Each enum is expected to carry a StdIOError variant holding anything convertible from StringBox<str> is preferred, since it is 8 bytes smaller and these errors sit in the Result of every write call. Pass Type => Variant explicitly if the variant is spelled differently.

#[derive(thiserror::Error, Eq, PartialEq, Clone, Debug)]
pub enum MyWritingError {
    #[error("IO error while writing: {0}")]
    StdIOError(Box<str>),
}
impl_from_io_error!(MyWritingError);