From effc95f2d1d55aa04bb975e439b98cbf159bbe81 Mon Sep 17 00:00:00 2001 From: Mingwei Samuel Date: Thu, 12 Sep 2024 13:06:58 -0700 Subject: [PATCH] docs: fix README.md doctests --- README.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9b6fec9..b7c75d6 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ `matchbox::match_deref!{...}` is a procedural macro, which allows you to use deref patterns right now in stable Rust. For example: -```rust +```rust,no_run use std::rc::Rc; enum Value { @@ -26,31 +26,37 @@ matchbox::match_deref!{ But there is a problem in my crate: all arms with `Deref @` are ignored when compiler performs exhaustiveness checking. So sometimes you will need to add `_ => unreachable!()` to the end. -I. e. it is possible that your arms are exhaustive, but the compiler will not be able to check this. But it is not possible that you arms are not exhaustive and the compiler will falsely report them as exhaustive. +I.e. it is possible that your arms are exhaustive, but the compiler will not be able to check this. But it is not possible that you arms are not exhaustive and the compiler will falsely report them as exhaustive. (I decided not to implement full exhaustiveness checking, because I hope that truly native support for deref patterns will be implemented in the rustc soon, so my work will be unneeded anyway. But if you want to implement similar macro with full exhaustiveness checking, go ahead, I can even link to your project here.) The macro calls `Deref::deref` internally. Keep in mind that `Deref::deref` takes REFERENCE to smart pointer and returns REFERENCE to pointee. So this code will work: `match &Nil { Deref @ x => ... }`, but this will not: `match Nil { Deref @ x => ... }`. Consider this code: -```rust +```rust,ignore matchbox::match_deref!{ match v { - Symbol(Deref @ x) => some_code_here, - _ => other_code_here, + Symbol(Deref @ x) => { + // some_code_here + } + _ => { + // other_code_here, + } } } ``` It will be desugared to something like this: -```rust +```rust,ignore match v { Symbol(a0) if (if let x = Deref::deref(a0) { true } else { false }) => if let x = Deref::deref(a0) { - some_code_here + // some_code_here } else { panic!() - }, - _ => other_code_here, + } + _ => { + // other_code_here + } } ```