Crate mono_macro
source ·Expand description
Mono macro
This crate provides the #[mono]
macro to force a generic function to be monomorphizied with given types.
Pair with share-generics
mode in rustc, this can result less code, for details see https://github.com/rust-lang/rust/pull/48779.
[dependencies]
mono-macro = "1.0"
Usage
Since we are monomorphizing ourselves, you are required to spell out the static dispatch manually:
In a bare function case,
use mono_macro::mono;
#[mono(T = i32, U = i64)]
fn func<T, U>(t: T, u: U) {}
it will be expanded to:
pub const _: *const () = (&func::<i32, i64>) as *const _ as _;
fn func<T, U>(t: T, u: U) {}
For more complicated case, use mono_macro!
instead:
use mono_macro::mono_macro;
trait Tr<T> {
fn foo(&self, _t: T) {}
}
struct Foo<'a> {
t: &'a str,
}
impl<'a, T> Tr<T> for Foo<'a> {
fn foo(&self, _t: T) {}
}
mono_macro!(<Foo<'static> as Tr<i32>>::foo);
this will expand to:
trait Tr<T> {
fn foo(&self, _t: T) {}
}
struct Foo<'a> {
t: &'a str,
}
impl<'a, T> Tr<T> for Foo<'a> {
fn foo(&self, _t: T) {}
}
pub const _: *const () = (&<Foo<'static> as Tr<i32>>::foo) as *const _ as _;
Macros
- Force monomorphizing on a path of function, for the complex functions like impl methods of generic types. For example,
Attribute Macros
- Apply this macro on a generic function will cast the bare function pointer with given type into pointer, which forces this function to be monomorphized.