match_cast

Crates.iomatch_cast
lib.rsmatch_cast
version0.1.2
sourcesrc
created_at2016-09-23 13:16:06.652004
updated_at2016-09-23 13:52:31.331171
descriptionMacro to match through Any types
homepage
repositoryhttps://github.com/DenisKolodin/match_cast
max_upload_size
id6596
size3,212
Denis Kolodin (therustmonk)

documentation

README

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);
}
Commit count: 5

cargo fmt