Skip to content

csmoe/mono-macro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 rust-lang/rust#48779.

[dependencies]
mono-macro = "0.1"

Usage

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 _;

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Force monomorphizing on a generic function.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages