2 releases

Uses old Rust 2015

0.1.2 Sep 23, 2016
0.1.1 Sep 23, 2016
0.1.0 Sep 23, 2016
Download history 14/week @ 2023-10-23 20/week @ 2023-10-30 14/week @ 2023-11-06 13/week @ 2023-11-13 20/week @ 2023-11-20 22/week @ 2023-11-27 10/week @ 2023-12-04 17/week @ 2023-12-11 13/week @ 2023-12-18 19/week @ 2023-12-25 8/week @ 2024-01-01 17/week @ 2024-01-08 14/week @ 2024-01-15 12/week @ 2024-01-22 15/week @ 2024-01-29 12/week @ 2024-02-05

57 downloads per month
Used in funki_templates

MIT/Apache

4KB

match_cast

This is a minimal crate which implements match through different types.

Usage:

#[macro_use]
extern crate match_cast;
use std::panic;

fn main() {
    let res = panic::catch_unwind(|| {
        panic!("Oh no!");
    });

    let any = res.unwrap_err();

    let result = match_cast!( any {
        val as Option<u8> => {
            format!("Option<u8> = {:?}", val)
        },
        val as String => {
            format!("String = {:?}", val)
        },
        val as &'static str => {
            format!("&'static str = {:?}", val)
        },
    });

    assert_eq!(result.unwrap(), "&'static str = \"Oh no!\"");
}

To use pattern there is match_down macro:

#[macro_use]
extern crate match_cast;
use std::any::Any;

struct Bar {
    x: u8,
}

struct Foo {
    x: u8,
}

fn main() {

    let any: Box<Any> = Box::new(Foo { x: 45 });

    let result = match_down!( any {
        Bar { x } => { x },
        Foo { x } => { x },
    });

    assert_eq!(result.unwrap(), 45);
}

No runtime deps