Skip to content

Commit

Permalink
Update usage of syn in Procedural Macros chapter
Browse files Browse the repository at this point in the history
This makes 2 changes:
1. It updates the versions of `syn` and `quote` to the latest versions.
2. It updates the code to not use the deprecated type alias `syn::MacroInput` and instead use the preferred `syn::DeriveInput`. `MacroInput` will be removed in the next breaking version of `syn`. See dtolnay/syn#173
  • Loading branch information
mystor authored Jun 7, 2017
1 parent 7a999ef commit 94946f2
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions first-edition/src/procedural-macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ In this case, we're keeping it as simple as possible.
Great, so let's write `impl_hello_world(&ast)`.

```rust,ignore
fn impl_hello_world(ast: &syn::MacroInput) -> quote::Tokens {
fn impl_hello_world(ast: &syn::DeriveInput) -> quote::Tokens {
let name = &ast.ident;
quote! {
impl HelloWorld for #name {
Expand All @@ -167,7 +167,7 @@ fn impl_hello_world(ast: &syn::MacroInput) -> quote::Tokens {

So this is where quotes comes in. The `ast` argument is a struct that gives us
a representation of our type (which can be either a `struct` or an `enum`).
Check out the [docs](https://docs.rs/syn/0.10.5/syn/struct.MacroInput.html),
Check out the [docs](https://docs.rs/syn/0.11.11/syn/struct.DeriveInput.html),
there is some useful information there. We are able to get the name of the
type using `ast.ident`. The `quote!` macro lets us write up the Rust code
that we wish to return and convert it into `Tokens`. `quote!` lets us use some
Expand All @@ -181,8 +181,8 @@ So I think that's it. Oh, well, we do need to add dependencies for `syn` and

```toml
[dependencies]
syn = "0.10.5"
quote = "0.3.10"
syn = "0.11.11"
quote = "0.3.15"
```

That should be it. Let's try to compile `hello-world`.
Expand Down Expand Up @@ -254,7 +254,7 @@ But how do we tell the user, that we do not accept enums?
The idiomatic way to report errors in procedural macros is to panic:
```rust,ignore
fn impl_hello_world(ast: &syn::MacroInput) -> quote::Tokens {
fn impl_hello_world(ast: &syn::DeriveInput) -> quote::Tokens {
let name = &ast.ident;
// Check if derive(HelloWorld) was specified for a struct
if let syn::Body::Struct(_) = ast.body {
Expand Down

0 comments on commit 94946f2

Please sign in to comment.