john01dav/polyerror

Are there will be work on eliminating dependencies in future?

Opened this issue · 1 comments

It loads a lot of deps diring build. Maybe them is not that necessary?

For example, this version support almost anything except support for generics and paths:

macro_rules! create_error {
    ($qual:vis $error_name:ident: $($variant_name:ident),*) => {
        #[derive(Debug)]
        $qual enum $error_name{
            $( $variant_name($variant_name), )* 
        }

        impl ::std::fmt::Display for $error_name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{:?}", self)
            }
        }
        impl ::std::error::Error for $error_name {}

        $(
        impl ::std::convert::From<$variant_name> for $error_name {
            fn from(error: $variant_name) -> Self {
                Self::$variant_name(error)
            }
        }
        )*
    };
}

And it would be faster to compile (since the compiler code is enough to handle this). Maybe it can be ever improved.

With this macro such code

fn main() {
    #[derive(Debug)]
    struct A;
    #[derive(Debug)]
    struct B;

    create_error!(pub HelloWorld: );
    create_error!(pub(crate) AnotherWorld: A, B);
    create_error!(MyWorld: A);
}

transform into the:

fn main() {
    #[derive(Debug)]
    struct A;
    #[derive(Debug)]
    struct B;

    pub enum HelloWorld {}

    impl ::std::fmt::Display for HelloWorld {
       fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{:?}", self)
       }
    }
    impl ::std::error::Error for HelloWorld {}
    pub(crate) enum AnotherWorld {
        A(A),
        B(B),
    }
    impl ::std::fmt::Display for AnotherWorld {
       fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{:?}", self)
       }
    }
    impl ::std::error::Error for AnotherWorld {}
    impl ::std::convert::From<A> for AnotherWorld {
        fn from(error: A) -> Self {
            Self::A(error)
        }
    }
    impl ::std::convert::From<B> for AnotherWorld {
        fn from(error: B) -> Self {
            Self::B(error)
        }
    }
    enum MyWorld {
        A(A),
    }
    impl ::std::fmt::Display for MyWorld {
       fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
                write!(f, "{:?}", self)
       }
    }
    impl ::std::error::Error for MyWorld {}
    impl ::std::convert::From<A> for MyWorld {
        fn from(error: A) -> Self {
            Self::A(error)
        }
    }
}