Skip to main content

fracints_macros/
lib.rs

1extern crate proc_macro;
2use fracints_internals::*;
3use proc_macro::TokenStream;
4
5macro_rules! impl_fracint_macros {
6($($ty:ident $from_str: ident);*;) => {$(
7    /// Literal conversion into the type. See `fiN::from_str` for the definition.
8    #[proc_macro]
9    pub fn $ty(input: TokenStream) -> TokenStream {
10        // we can't get an exact char-for-char copy but this gets close enough
11        let mut s = String::new();
12        for token in input {
13            s.push_str(&token.to_string())
14        }
15        for c in s.chars() {
16            if c == '"' {
17                panic!("The literal input to this macro should not include quotation marks");
18            }
19        }
20        match $from_str(&s) {
21            Ok(x) => format!("{}({})", stringify!($ty), x).parse().unwrap(),
22            Err(e) => panic!("Invalid `{}` string representation: {}", stringify!($ty), e,),
23        }
24    }
25)*}
26}
27
28impl_fracint_macros!(
29    fi8 i8_from_str;
30    fi16 i16_from_str;
31    fi32 i32_from_str;
32    fi64 i64_from_str;
33    fi128 i128_from_str;
34);