From a5fbc1eb871623365a6289f2ad061c80a89996d5 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Tue, 11 Dec 2018 14:05:18 -0500 Subject: [PATCH] Fix function like example Fixes #285 --- posts/2018-10-25-Rust-1.30.0.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/posts/2018-10-25-Rust-1.30.0.md b/posts/2018-10-25-Rust-1.30.0.md index d05450c03..3e8a2736f 100644 --- a/posts/2018-10-25-Rust-1.30.0.md +++ b/posts/2018-10-25-Rust-1.30.0.md @@ -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, + bar: RefCell, + } + + 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