#match #any #traits #expressions

macro match_any_trait

match expressions for any trait

1 unstable release

0.1.1 Sep 14, 2023
0.1.0 Sep 14, 2023
0.0.0 Sep 14, 2023

#496 in Procedural macros

Download history 10/week @ 2024-02-21 11/week @ 2024-02-28 2/week @ 2024-03-13 18/week @ 2024-03-27 33/week @ 2024-04-03

51 downloads per month

Apache-2.0

8KB
119 lines

match_any_trait

Provides a procedural macro, that is match expressions for any trait

Install

cargo add match_any_trait

Example

use std::any::Any;
use match_any_trait::match_any_trait;

#[derive(Debug)]
struct MyStruct {
    x: u32,
}

#[derive(Debug)]
struct MyNum {
    y: u32,
}

#[derive(Debug)]
struct MyTuple;

#[derive(Debug)]
struct MyTemplate;

fn main() {
    // let num = MyStruct{
    //     x: 1,
    // };

    // let num = MyNum{
    //     y: 1,
    // };

    // let num = MyTuple;

    let num = MyTemplate;

    let num = &num as &dyn Any;

    match_any_trait! {
        match num {
            MyStruct(s) | MyNum(s) => println!("num is a {:?}", s),
            MyTuple => println!("num is a MyTuple") 
            MyTemplate => println!("num is a MyTemplate"),
            _ => println!("num is unknown"),
        }
    }

}

Dependencies

~320–770KB
~18K SLoC