4 releases

0.1.3 Feb 28, 2022
0.1.2 Feb 11, 2022
0.1.1 Feb 11, 2022
0.1.0 Feb 11, 2022

#2395 in Rust patterns

MIT license

2KB

monomo

Easy trait monomoprhization crate. Especially useful with typetag crate.

Example

Suppose we have a trait Foo<T> and we would like to monomorphize it in order to use it with typetag, as we cannot use generic trait.

trait Foo<T> {}

// Notice that you don't have to use typetag attribute, 
// and the trait will still be monomorphized. 
// typetag is just good example when you would like to use monomo.
monomo::rphize!(
    #[typetag::serde]
    Foo<i32>
);

Above macro expands to monomorphized version of Foo<i32>.

In order to implement monomorphized trait for a struct we can do the following:

// Notice that you don't have to use typetag attribute, 
// and the trait will still be monomorphized. 
// typetag is just good example when you would like to use monomo.
#[monomo::rphize_impl]
#[typetage::serde]
impl Foo<i32> for Bar {
    // ...
}

Thanks to this you can later use Foo<i32> implementors as the monomorphized trait object.

struct BarContainer {
    bars: Vec<monomo::rph!(Foo<i32>)>
}

Dependencies

~1.5MB
~38K SLoC