match_any_trait

Crates.iomatch_any_trait
lib.rsmatch_any_trait
version0.1.1
sourcesrc
created_at2023-09-14 08:52:56.736118
updated_at2023-09-14 14:25:31.585921
descriptionmatch expressions for any trait
homepage
repositoryhttps://github.com/mingshuisheng/match_any_trait
max_upload_size
id972431
size18,790
(mingshuisheng)

documentation

README

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"),
        }
    }

}
Commit count: 2

cargo fmt