Fixed some small issues

This commit is contained in:
Jakob Demler 2017-02-19 18:15:44 +01:00
parent 97451996e6
commit 198208be0e

View File

@ -210,12 +210,15 @@ Hello, World! My name is FrenchToast
Hello, World! My name is Waffles Hello, World! My name is Waffles
``` ```
We've done it!
## Custom Attributes ## Custom Attributes
In some cases it might make sense to allow users some kind of configuration. In some cases it might make sense to allow users some kind of configuration.
For our example the user might want to overwrite the name that is printed in the `hello_world()` method. For example, the user might want to overwrite the name that is printed in the `hello_world()` method.
This can be achieved with custom attributes: This can be achieved with custom attributes:
```rust,ignore ```rust,ignore
#[derive(HelloWorld)] #[derive(HelloWorld)]
#[HelloWorldName = "the best Pancakes"] #[HelloWorldName = "the best Pancakes"]
@ -232,8 +235,8 @@ If we try to compile this though, the compiler will respond with an error:
error: The attribute `HelloWorldName` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642) error: The attribute `HelloWorldName` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642)
``` ```
The compiler needs to know that we handle this attribute and to not respond with an error. The compiler needs to know that we're handling this attribute and to not respond with an error.
This is done in the `hello-world-derive`-crate by adding `attributes` to the `proc_macro_derive` attribute: This is done in the `hello-world-derive` crate by adding `attributes` to the `proc_macro_derive` attribute:
```rust,ignore ```rust,ignore
#[proc_macro_derive(HelloWorld, attributes(HelloWorldName))] #[proc_macro_derive(HelloWorld, attributes(HelloWorldName))]
@ -244,11 +247,11 @@ Multiple attributes can be specified that way.
## Raising Errors ## Raising Errors
Let's assume that we do not want to accept `Enums` as input to our custom derive method. Let's assume that we do not want to accept enums as input to our custom derive method.
This condition can be easily checked with the help of `syn`. This condition can be easily checked with the help of `syn`.
But how to we tell the user, that we do not accept `Enums`. But how do we tell the user, that we do not accept enums?
The idiomatic was to report errors in procedural macros is to panic: The idiomatic way to report errors in procedural macros is to panic:
```rust,ignore ```rust,ignore
fn impl_hello_world(ast: &syn::MacroInput) -> quote::Tokens { fn impl_hello_world(ast: &syn::MacroInput) -> quote::Tokens {