Document the lazy-static example
diff --git a/examples/lazy-static/README.md b/examples/lazy-static/README.md
new file mode 100644
index 0000000..611c055
--- /dev/null
+++ b/examples/lazy-static/README.md
@@ -0,0 +1,42 @@
+An example of parsing a custom syntax within a `functionlike!(...)` procedural
+macro. Demonstrates how to trigger custom warnings and error messages on
+individual tokens of the input.
+
+- [`lazy-static/src/lib.rs`](lazy-static/src/lib.rs)
+- [`example/src/main.rs`](example/src/main.rs)
+
+The library implements a `lazy_static!` macro similar to the one from the real
+[`lazy_static`](https://docs.rs/lazy_static/1.0.0/lazy_static/) crate on
+crates.io.
+
+```rust
+lazy_static {
+ static ref USERNAME: Regex = Regex::new("^[a-z0-9_-]{3,16}$").unwrap();
+}
+```
+
+Compile and run the example by doing `cargo run` in the directory of the
+`example` crate.
+
+The implementation shows how to trigger custom warnings and error messages on
+the macro input. For example if you try adding an uncreatively named `FOO` lazy
+static, the macro will scold you with the following warning.
+
+```
+warning: come on, pick a more creative name
+ --> src/main.rs:10:16
+ |
+10 | static ref FOO: String = "lazy_static".to_owned();
+ | ^^^
+```
+
+And if you try to lazily initialize `() = ()`, the macro will outright refuse
+the compile it for you.
+
+```
+error: I can't think of a legitimate use for lazily initializing the value `()`
+ --> src/main.rs:10:27
+ |
+10 | static ref UNIT: () = ();
+ | ^^
+```
diff --git a/examples/lazy-static/example/src/main.rs b/examples/lazy-static/example/src/main.rs
index 9a41b6f..16cfb36 100644
--- a/examples/lazy-static/example/src/main.rs
+++ b/examples/lazy-static/example/src/main.rs
@@ -20,5 +20,6 @@
}
fn validate(name: &str) {
+ // The USERNAME regex is compiled lazily the first time its value is accessed.
println!("is_match({:?}): {}", name, USERNAME.is_match(name));
}
diff --git a/examples/lazy-static/lazy-static/src/lib.rs b/examples/lazy-static/lazy-static/src/lib.rs
index 834ee9f..ba2d5b7 100644
--- a/examples/lazy-static/lazy-static/src/lib.rs
+++ b/examples/lazy-static/lazy-static/src/lib.rs
@@ -56,10 +56,10 @@
.emit();
}
- if let Type::Tuple(ref ty) = ty {
- if ty.elems.is_empty() {
- ty.span().unstable()
- .error("I can't think of a legitimate use for lazily initializing `()`")
+ if let Expr::Tuple(ref init) = init {
+ if init.elems.is_empty() {
+ init.span().unstable()
+ .error("I can't think of a legitimate use for lazily initializing the value `()`")
.emit();
return TokenStream::empty();
}