Skip to content

Fix function like example #328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions posts/2018-10-25-Rust-1.30.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,30 @@ thing the attribute is attached to, in this case, `fn index() {}` and the rest
of the function's body.

Function-like macros define macros that look like function calls. For
example, an `sql!` macro:
example, the [`gnome-class` crate](https://gitlab.gnome.org/federico/gnome-class)
has a procedural macro that defines `GObject` classes in Rust:

```rust
let sql = sql!(SELECT * FROM posts WHERE id=1);
gobject_gen!(
class MyClass: GObject {
foo: Cell<i32>,
bar: RefCell<String>,
}

impl MyClass {
virtual fn my_virtual_method(&self, x: i32) {
// ... do something with x ...
}
}
)
```

This macro would parse the SQL statement inside of it and check that it's
syntactically correct. This macro would be defined like this:
This looks like a function that is taking a bunch of code as an argument.
This macro would be defined like this:

```
#[proc_macro]
pub fn sql(input: TokenStream) -> TokenStream {
pub fn gobject_gen(input: TokenStream) -> TokenStream {
```

This is similar to the derive macro's signature: we get the tokens that
Expand Down