#pattern-match #deref #rc #stable #now #string

macro match_deref

Deref patterns in "match" for stable Rust. Now you can match through Rc, String, etc

2 releases

0.1.2 Dec 28, 2024
0.1.1 Jul 29, 2022
0.1.1-alpha.1 Jul 28, 2022
0.1.0 Jul 28, 2022

#1426 in Procedural macros

Download history 2/week @ 2024-09-17 14/week @ 2024-09-24 4/week @ 2024-10-01 49/week @ 2024-10-08 14/week @ 2024-10-15 3/week @ 2024-10-22 5/week @ 2024-10-29 38/week @ 2024-11-05 31/week @ 2024-11-12 7/week @ 2024-11-19 51/week @ 2024-11-26 3/week @ 2024-12-03 55/week @ 2024-12-10 9/week @ 2024-12-17 127/week @ 2024-12-24 4/week @ 2024-12-31

198 downloads per month
Used in 2 crates

MIT/Apache

16KB
316 lines

Deref patterns in match for stable Rust. Now you can match through Rc, String, etc.

(Note: as on 2024-12-28 nightly has expiremental support for deref patterns, see end of this README for details.)

match_deref::match_deref!{...} is a procedural macro, which allows you to use deref patterns right now in stable Rust.

For example:

use std::rc::Rc;

enum Value {
    Nil,
    Cons(Rc<Value>, Rc<Value>),
    Symbol(String),
}

use Value::*;

fn main() {
    let v: &Value = todo!();
    match_deref::match_deref!{
        match v {
            Nil => todo!(),
            Cons(Deref @ Symbol(Deref @ "quote"), Deref @ Cons(x, Deref @ Nil)) => todo!(),
            _ => todo!(),
        }
    }
}

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 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:

match_deref::match_deref!{
    match v {
        Symbol(Deref @ x) => some_code_here,
        _ => other_code_here,
    }
}

It will be desugared to something like this:

match v {
    Symbol(a0) if (if let x = Deref::deref(a0) { true } else { false }) => if let x = Deref::deref(a0) {
        some_code_here
    } else {
        panic!()
    },
    _ => other_code_here,
}

My macro is hygienic, i. e. everything will work even if you use variable named a0

As on 2024-12-28 nightly has expiremental support for deref patterns. This is how you can use it (without this crate):

#![feature(deref_patterns)]
#![feature(string_deref_patterns)]
#![allow(incomplete_features)]

use std::rc::Rc;

enum Value {
    Nil,
    Cons(Rc<Value>, Rc<Value>),
    Symbol(String),
}

use Value::*;

fn main() {
    let v: Value = todo!();
    match v {
        Nil => todo!(),
        Cons(deref!(Symbol("quote")), deref!(Cons(x, deref!(Nil)))) => todo!(),
        _ => todo!(),
    }
}

You don't need to be registered on SourceHut to create bug report.

If you think that this software is not needed or existing software already subsumes its functionality, please, tell me that, I will not be offended.

Dependencies

~1.5MB
~37K SLoC