spec-fn 1.0.0

A proof-of-concept crate of adding 'function specialization' to rust
Documentation

spec

A proof-of-concept crate of adding "function specialization" to rust.

The crate allows defining a function and its specialized cases, using rusts match statements to create a "specialized function". This has the neat side effect of getting exhaustive pattern enforcement by rust itself while staying light weight.

// This definition:
spec! {
    fn f(c: char) -> &'static str {}

    #[case('@')] {
        "[at]"
    }

    #[case(_)] #[when(c < 'a' || c > 'z')] {
        "not a lowercase letter"
    }

    #[case(_)] {
        "a lowercase letter"
    }
}

// Turns into this:
fn f(c: char) -> &'static str {
    #[allow(unused_parens)]
    match (c) {
        ('@') => "[at]",
        (_) if c < 'a' || c > 'z' => "not a lowercase letter",
        (_) => "a lowercase letter",
    }
}