Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Pub use impl::* to avoid two dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Feb 28, 2017
1 parent cf25adf commit 0d8b9f0
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 45 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "proc-macro-hack"
version = "0.2.4"
version = "0.3.0"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Procedural functionlike!() macros using only Macros 1.1"
repository = "https://github.com/dtolnay/proc-macro-hack"

[dependencies]
proc-macro-hack-impl = { version = "0.3", path = "impl" }
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ traits or functions or ordinary macros.
https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack

```rust
#[macro_use] extern crate proc_macro_hack;
#[macro_use] extern crate proc_macro_hack_impl;
#[macro_use]
extern crate proc_macro_hack;

#[macro_use]
extern crate demo_hack_impl;
pub use demo_hack_impl::*;

/// Add one to an expression.
proc_macro_expr_decl!(add_one! => add_one_impl);
Expand All @@ -43,8 +47,8 @@ and private modules are fine but nothing can be public.
https://github.com/dtolnay/proc-macro-hack/tree/master/demo-hack-impl

```rust
#[macro_use] extern crate proc_macro_hack;
#[macro_use] extern crate proc_macro_hack_impl;
#[macro_use]
extern crate proc_macro_hack;

proc_macro_expr_impl! {
/// Add one to an expression.
Expand All @@ -61,13 +65,11 @@ proc_macro_item_impl! {
}
```

Both crates depend on `proc-macro-hack` which itself has a declaration crate and
implementation crate:
Both crates depend on `proc-macro-hack`:

```toml
[dependencies]
proc-macro-hack = "0.2"
proc-macro-hack-impl = "0.2"
proc-macro-hack = "0.3"
```

Additionally, your implementation crate (but not your declaration crate) is a
Expand All @@ -80,15 +82,15 @@ proc-macro = true

## Using procedural macros

Users of your crate depend on your declaration crate and implementation crate,
then use your procedural macros as though it were magic. They even get
Users of your crate depend on your declaration crate (not your implementation
crate), then use your procedural macros as though it were magic. They even get
reasonable error messages if your procedural macro panics.

https://github.com/dtolnay/proc-macro-hack/tree/master/example

```rust
#[macro_use] extern crate demo_hack;
#[macro_use] extern crate demo_hack_impl;
#[macro_use]
extern crate demo_hack;

two_fn!(two);

Expand All @@ -102,8 +104,6 @@ fn main() {
## Limitations

- The input to your macro cannot contain dollar signs.
- Users of your macro need to `#[macro_use] extern crate` two different crates,
not just one.
- Your macro must expand to either an expression or zero-or-more items, cannot
sometimes be one or the other depending on input.

Expand Down
5 changes: 2 additions & 3 deletions demo-hack-impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "demo-hack-impl"
version = "0.0.1"
version = "0.0.2"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Demo of proc-macro-hack"
Expand All @@ -10,5 +10,4 @@ repository = "https://github.com/dtolnay/proc-macro-hack"
proc-macro = true

[dependencies]
proc-macro-hack = { version = "0.2", path = ".." }
proc-macro-hack-impl = { version = "0.2", path = "../impl" }
proc-macro-hack = { version = "0.3", path = ".." }
4 changes: 2 additions & 2 deletions demo-hack-impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[macro_use] extern crate proc_macro_hack;
#[macro_use] extern crate proc_macro_hack_impl;
#[macro_use]
extern crate proc_macro_hack;

proc_macro_expr_impl! {
/// Add one to an expression.
Expand Down
6 changes: 3 additions & 3 deletions demo-hack/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "demo-hack"
version = "0.0.1"
version = "0.0.2"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Demo of proc-macro-hack"
repository = "https://github.com/dtolnay/proc-macro-hack"

[dependencies]
proc-macro-hack = { version = "0.2", path = ".." }
proc-macro-hack-impl = { version = "0.2", path = "../impl" }
proc-macro-hack = { version = "0.3", path = ".." }
demo-hack-impl = { version = "0.0.2", path = "../demo-hack-impl" }
8 changes: 6 additions & 2 deletions demo-hack/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#[macro_use] extern crate proc_macro_hack;
#[macro_use] extern crate proc_macro_hack_impl;
#[macro_use]
extern crate proc_macro_hack;

#[macro_use]
extern crate demo_hack_impl;
pub use demo_hack_impl::*;

/// Add one to an expression.
proc_macro_expr_decl!(add_one! => add_one_impl);
Expand Down
2 changes: 0 additions & 2 deletions example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ publish = false

[dependencies]
demo-hack = { path = "../demo-hack" }
demo-hack-impl = { path = "../demo-hack-impl" }
proc-macro-hack = { path = ".." }
1 change: 0 additions & 1 deletion example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#[macro_use] extern crate demo_hack;
#[macro_use] extern crate demo_hack_impl;

two_fn!(two);

Expand Down
2 changes: 1 addition & 1 deletion impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "proc-macro-hack-impl"
version = "0.2.4"
version = "0.3.0"
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT/Apache-2.0"
description = "Procedural functionlike!() macros using only Macros 1.1"
Expand Down
8 changes: 0 additions & 8 deletions impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ use proc_macro::TokenStream;

#[proc_macro_derive(ProcMacroHackExpr)]
pub fn hack_expr(input: TokenStream) -> TokenStream {
if input.to_string().trim() == "struct Ignored;" {
return "".parse().unwrap();
}

let (name, name_impl) = names(input);

let rules = format!("
Expand All @@ -29,10 +25,6 @@ pub fn hack_expr(input: TokenStream) -> TokenStream {

#[proc_macro_derive(ProcMacroHackItem)]
pub fn hack_item(input: TokenStream) -> TokenStream {
if input.to_string().trim() == "struct Ignored;" {
return "".parse().unwrap();
}

let (name, name_impl) = names(input);

let rules = format!("
Expand Down
12 changes: 4 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
extern crate proc_macro;

#[macro_use]
extern crate proc_macro_hack_impl;
pub use proc_macro_hack_impl::*;

#[doc(hidden)]
pub use proc_macro::TokenStream;

Expand Down Expand Up @@ -35,10 +39,6 @@ macro_rules! proc_macro_expr_impl {
$( #[$attr] )*
#[proc_macro_derive($func)]
pub fn $func(input: $crate::TokenStream) -> $crate::TokenStream {
#[derive(ProcMacroHackExpr)]
struct Ignored;
let _ = Ignored;

let source = input.to_string();
let source = source.trim();

Expand Down Expand Up @@ -74,10 +74,6 @@ macro_rules! proc_macro_item_impl {
$( #[$attr] )*
#[proc_macro_derive($func)]
pub fn $func(input: $crate::TokenStream) -> $crate::TokenStream {
#[derive(ProcMacroHackItem)]
struct Ignored;
let _ = Ignored;

let source = input.to_string();
let source = source.trim();

Expand Down

0 comments on commit 0d8b9f0

Please sign in to comment.