[][src]Macro whiteout::eraser

macro_rules! eraser {
    ($name:ident, $($tr:tt)*) => { ... };
}

eraser!(name, trait) creates a function with the given identifier that erases values to an anonymous type that is impl Trait for the given trait.

Examples

#[macro_use]
extern crate whiteout;

// Define a custom trait into which types will be erased.
trait MyTrait: 
    std::ops::Add<Self, Output=Self>  // Allow the operation we need
    + std::convert::From<i32>  // Allow converting from concrete values
    + std::fmt::Debug  // Allow printing (for use with assert!())
    + PartialEq  // Allow comparison (for use with assert_eq!())
    {}

// Implement MyTrait for all possible types.
impl<T> MyTrait for T 
    where T: std::ops::Add<Self, Output=Self> 
    + std::convert::From<i32> 
    + std::fmt::Debug
    + PartialEq
    {}

// Create an eraser function for the custom trait
eraser!(erase_my_trait, MyTrait);

fn main() {
    // Use the eraser function. 
    // If we used erase!(10, MyTrait); for these
    // they would be of different types.
    let a = erase_my_trait(10);
    let b = erase_my_trait(5);
    assert_eq!(a + b, 15.into());
}