[enumx 0.4.1] enum trait delegation and `-> impl Trait`

Documentation

See the enumx book for more.

What's new

  1. macros for enum trait implementation delegated to its variants.
    Provides _Variants!(),_Variant!(),_variant!(),_match!(),_enum!() inside def_impls!{}.

    def_impls! {
        impl<T0,T1> Clone for Enum2<T0,T1>
            where _Variants!(): Clone
        {
            fn clone( &self ) -> Self {
                _match!(
                    _enum!( _variant!().clone() )
                )
            }
        }
    }
    

    Predefined impls for frequently used std traits:

    impl_trait!{ Iterator _for!( Type )}
    

    Note: User defined enums are also supported.

  2. Allows functions returning -> impl Trait to return different types as long as they implemented the trait.

    #[sum]
    fn f( cond: bool ) -> impl Clone {
        if cond {
            #[variant] 1_i32
        } else {
            #[variant] "false"
        }
    }
    
2 Likes

And one more thing: checked exception as impl Error.

Sometimes the downstream users do not bother "poluting" signatures of functions
which call upstream #[cex] APIs returning checked exceptions. If all variants
of the error Enum!() have implemented some famous trait, e.g.
std::error::Error, the downstream users get a chance to simply write
-> Result<_, impl std::error::Error> in their function signatures.

Example

use std::error::Error;
use enumx::export::*;
use enumx::predefined::*;
use cex::*;

impl Error for A { /* omitted */ }
impl Error for B { /* omitted */ }
impl Error for C { /* omitted */ }

#[cex] pub fn upstream() -> Result!( () throws A, B, C );

fn downstream() -> Result<(), impl Error> {
    Ok( upstream()? )
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.