Pre-RFC: Custom suffixes for integer and float literals

Custom suffix is definitely not just a feature wanted by dimensioned types, for instance it was wanted by fixed point types and primitives of other bitness (f16, f80, u256, u31).

Expanding a bit, custom suffix can also be found outside of integer or floating point, e.g. strings ("foo"s), chars ('c'_ascii), bytes (b'@'u16), and byte strings (b"foo\xff"_big_endian).

These literals may also be needed as a pattern, not just expressions:

match some_256bit_number {
    0u256 => None,
    c => Some(c),
}

IMO a literal suffix should be a procedural macro instead of a const fn, so interpolated strings ("log {date:?}: {msg}"_fmt) can be supported.

use proc_macro::Span;

#[proc_macro_int_lit_suffix]
pub fn km(literal: &str, span: Span) -> TokenTree {
    let value = literal.parse::<u128>().unwrap();
    let value = Literal::f64_unsuffixed_with_span((value * 1000) as f64, span);
    quote!{ mylib::si::Meter(#value) }
}
2 Likes