2 releases

0.1.1 Jan 3, 2024
0.1.0 Dec 17, 2023

#11 in #either

MIT license

18KB
343 lines

or_gen Proc Macro Documentation

A proc macro that converts if or match expressions that return multiple types into Or types.

Usage Examples

For if Expression

#![feature(proc_macro_hygiene)]

use or_rs_macros::or_gen;
use or_rs::enums::Or3;

#[or_gen]
// Add a type annotation explicitly
let s: Or3<i32, String, f32> = if true {
    3
} else if false {
    "hello".to_string()
} else {
    3.0
};

For match Expression

#![feature(proc_macro_hygiene)]

use or_rs_macros::or_gen;
use or_rs::enums::Or3;

#[or_gen]
// Add a type annotation explicitly
let s: Or3<i32, f32, String> = match 42 {
    1  => 22,
    10 => 3.2,
    _  => "hello".to_string(),
};

Function Definition

#[proc_macro_attribute]
pub fn or_gen(_attr: TokenStream, item: TokenStream) -> TokenStream {
    parser::MacroParser::parse(item)
}

lib.rs:

Macros for use with Or type.

Dependencies

~0.2–10MB
~58K SLoC