Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/csmoe/mono-macro

Force monomorphizing on a generic function.
https://github.com/csmoe/mono-macro

Last synced: 21 days ago
JSON representation

Force monomorphizing on a generic function.

Awesome Lists containing this project

README

        

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.

```toml
[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,
```rust
#[mono(T = i32, U = i64)]
fn func(t: T, u: U) {
...
}
```

it will be expanded to:
```rust
pub const _: *const () = (&foo::) as *const _ as _;
fn func(t: T, u: U) {
...
}

```
For more complicated case, use `mono_macro!` instead:
```rust
trait Tr {
fn foo(&self, _t: T) {}
}

struct Foo<'a> {
t: &'a str,
}

impl<'a, T> Tr for Foo<'a> {
fn foo(&self, _t: T) {}
}

mono_macro!( as Tr>::foo);
```

this will expand to:
```rust
trait Tr {
fn foo(&self, _t: T) {}
}

struct Foo<'a> {
t: &'a str,
}

impl<'a, T> Tr for Foo<'a> {
fn foo(&self, _t: T) {}
}

pub const _: *const () = (& as Tr>::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.