Macro tear::anybox[][src]

macro_rules! anybox {
    ( $e:expr ) => { ... };
}

Turn a value into a Box<dyn Any>

Description

Give it a value or an expression and it will turn it into a Box<dyn Any> value.

Used for breaking multiple loops with different values types with twist!.

Examples

Just wrapping the value and getting it back.

use tear::anybox;

let boxed = anybox!(3);
let x = match boxed.downcast::<i32>() {
    Ok(v) => *v,
    Err(_) => panic!("Failed to get the integer back."),
};

assert_eq![ x, 3 ];

Using it as the breakval with twist!.

use tear::{twist, anybox};
use tear::Looping;

let e = Looping::BreakVal { label: Some(0), value: anybox!("a".to_string()) };

let x = 'a: loop {
    let _ = 'b: loop {
        twist! { -box -val i32, -label 'a: String | e }
        break 0;
    };
    break "b".to_string();
};
assert_eq![ x, "a".to_string() ];