Skip to content

Commit

Permalink
Revive has_self (#11)
Browse files Browse the repository at this point in the history
* Release "engineering" (rustfmt, cargo, readme)

* Revive has_self and update to watt 0.4

This is not a breaking change. I think.

* Basic docwork

* README: drive-by typo fix
  • Loading branch information
Artoria2e5 authored Aug 15, 2021
1 parent 298b0ff commit d9c1590
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 84 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
**/*.rs.bk
Cargo.lock
wasm/target
wasm/Cargo.lock
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ authors = ["Andre Bogus <[email protected]>"]
description = "A procedural macro to de-monomorphize generic methods"
edition = "2018"
keywords = ["monomorphization", "compiler-plugin"]
categories = ["development-tools", "wasm", "rust-patterns"]
license = "Apache-2.0"
name = "momo"
readme = "README.md"
repository = "https://github.com/llogiq/momo"
version = "0.2.1"
version = "0.2.2"

[lib]
proc_macro = true

[dependencies]
watt = "0.1"
watt = "0.4"

35 changes: 27 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# momo

### Keep your compile time during MOnoMOrphization
**Keep your compile time during MOnoMOrphization**
[![badge](https://docs.rs/momo/badge.svg)](https://docs.rs/momo)

This is a `proc_macro` crate to help keeping the code footprint of
generic methods in check. Often, generics are used in libraries to
Expand All @@ -11,23 +11,42 @@ that makes the code less readable.

Add a `#[momo]` annotation from this crate to split your function
into an outer conversion and a private inner function. In return,
you get some compile time for a tiny bit of runtime (if at all) –
you get some compile time for a tiny bit of runtime (if at all) –
without impairing readability.

For now, the only place where we can put the `#[momo]` annotations
is on plain functions.
Conversions currently supported are `Into` (`.into()`), `AsRef`
(`.as_ref()`), and `AsMut` (`.as_mut()`). See `enum Conversions`
in code.


This new updated version usesi D. Tolnay's [watt] runtime to speed
## Notes on watt

This new updated version uses D. Tolnay's [watt] runtime to speed
up the compile time, which was negatively affected with proc macro
baggage. Rebuilding the wasm can be done with the commands:
baggage.

The main crate uses a pre-built wasm containing the tagged version.
Rebuilding the wasm can be done with the commands:

```bash
cd wasm
cargo build --release --target wasm32-unknown-unknown
cp wasm/target/wasm32-unknown-unknown/release/momo.wasm ../src
# If wasm-opt is unavailable, copying the file is fine.
wasm-opt --strip-debug -Oz target/wasm32-unknown-unknown/release/momo.wasm -o ../src/momo.wasm
```

You might need to add the `wasm32-unknown-unknown` target to your
Rust toolchain.

[watt]: https://github.com/dtolnay/watt

(If you are tagging a new version, remember to commit the new `wasm` file.
Also change the versions in both `Cargo.toml` files.)

## Debugging the macro

The [cargo-expand] tool may be used to expand the output of macro expansion,
including from this proc-macro. To examine the results of the example file,
use `cargo expand --example check`.

[cargo-expand]: https://github.com/dtolnay/cargo-expand
27 changes: 27 additions & 0 deletions examples/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,32 @@ fn main() {
let mut m = [3usize, 42];
assert_eq!(check_generic(i, s, &mut m), 16);
assert_eq!(check_impl_trait(i, s, &mut m), 16);

let check = Check;
assert_eq!(check.generic(i, s, &mut m), 16);
assert_eq!(check.impl_trait(i, s, &mut m), 16);
}

pub struct Check;

impl Check {
#[momo]
pub fn generic<I: Into<usize>, S: AsRef<str>, M: AsMut<[usize]>>(
&self,
i: I,
s: S,
mut m: M,
) -> usize {
i.into() + s.as_ref().len() + m.as_mut()[0]
}

#[momo]
pub fn impl_trait(
&self,
i: impl Into<usize>,
s: impl AsRef<str>,
mut m: impl AsMut<[usize]>,
) -> usize {
i.into() + s.as_ref().len() + m.as_mut()[0]
}
}
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
extern crate proc_macro;

#![doc = include_str!("../README.md")]
use proc_macro::TokenStream;
use watt::WasmMacro;

static WASM: &[u8] = include_bytes!("momo.wasm");
static MACRO: WasmMacro = WasmMacro::new(WASM);

#[proc_macro_attribute]
/// Generate lightweight monomorphized wrapper around main implementation.
/// May be applied to functions and methods.
pub fn momo(attrs: TokenStream, input: TokenStream) -> TokenStream {
watt::proc_macro_attribute("momo", input, attrs, WASM)
MACRO.proc_macro_attribute("momo", input, attrs)
}

Binary file modified src/momo.wasm
Binary file not shown.
9 changes: 5 additions & 4 deletions wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
authors = ["Andre Bogus <[email protected]>"]
description = "A procedural macro to de-monomorphize generic methods"
description = "A procedural macro to de-monomorphize generic methods [internal WASM implementation]"
edition = "2018"
keywords = ["monomorphization", "compiler-plugin"]
categories = ["development-tools", "wasm", "rust-patterns"]
license = "Apache-2.0"
name = "momo"
name = "momo-watt"
readme = "README.md"
repository = "https://github.com/llogiq/momo"
version = "0.1.0"
version = "0.2.2"

[profile.release]
opt-level = "z"
Expand All @@ -23,4 +24,4 @@ proc-macro2 = { git = "https://github.com/dtolnay/watt" }
[dependencies]
syn = { version = "1.0", features = ["full", "fold"] }
quote = "1.0"
proc-macro2 = "1.0"
proc-macro2 = "=1.0.27" # Temporary hack as watt is left at .27
Loading

0 comments on commit d9c1590

Please sign in to comment.