Crates.io | mono-macro |
lib.rs | mono-macro |
version | 0.1.3 |
source | src |
created_at | 2022-03-06 06:51:39.801357 |
updated_at | 2023-08-08 12:23:24.199552 |
description | Force monomorphizing on functions with `share-generics` to minimalize binary size. |
homepage | |
repository | https://github.com/csmoe/mono-macro |
max_upload_size | |
id | 544408 |
size | 22,029 |
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 = "0.1"
Since we are monomorphizing ourselves, you are required to spell out the static dispatch manually:
In a bare function case,
#[mono(T = i32, U = i64)]
fn func<T, U>(t: T, u: U) {
...
}
it will be expanded to:
pub const _: *const () = (&foo::<i32, i64>) as *const _ as _;
fn func<T, U>(t: T, u: U) {
...
}
For more complicated case, use mono_macro!
instead:
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 _;