#sum #anonymous #error #type #derive-debug #debugging #io-error

macro some_error_fork

A library for creating and using anonymous sum types as errors in Rust

1 unstable release

0.5.1 Nov 25, 2024

#1707 in Procedural macros

Download history 142/week @ 2024-11-23 18/week @ 2024-11-30

160 downloads per month

MIT license

10KB
157 lines

Copied from https://github.com/jam1garner/some-error/tree/master.

Source code slightly adapted to derive the Debug trait.

This package is a temp workaround until Anonymous Sum Types RFC is implemented (which is unlikely to happen any time soon).


lib.rs:

A library for allowing Anonymous Sum Types in the error of the returned result. The function this is attached to must return a type in the form of Result<T, E1 + E2 + ...>.

Example:

use std::io;
use some_error::*;

#[derive(Debug, Clone, Copy)]
struct NotZeroError(u32);

#[some_error]
fn my_func() -> Result<(), io::Error + NotZeroError>{
    let x = 3;
    if x != 0 {
        Err(NotZeroError(x))?;
    }

    Ok(())
}

fn main() {
    match my_func() {
        Ok(_) => {
            println!("Worked ok!");
        }
        Err(my_func::NotZeroError(NotZeroError(x))) => {
            println!("{} is not zero!!", x);
        }
        Err(my_func::io::Error(io_err)) => {
            println!("io error: {:?}", io_err);
        }
    }
}

Dependencies

~1.5MB
~37K SLoC