-
Notifications
You must be signed in to change notification settings - Fork 24
Description
Proposal
Problem statement
I made a crate culit
that lets you have custom literals:
use culit::culit;
use std::num::NonZeroUsize;
#[culit]
fn main() {
// custom literal, ends with "nzusize". Expand to `custom_literal::integer::nzusize!(100)`
assert_eq!(100nzusize, NonZeroUsize::new(100).unwrap());
// COMPILE ERROR!
// let illegal = 0nzusize;
}
mod custom_literal {
pub mod integer {
macro_rules! nzusize {
// handle `0` specially
(0) => {
compile_error!("`0` is not a valid `NonZeroUsize`")
};
($value:literal) => {
const { NonZeroUsize::new($value).unwrap() }
};
}
pub(crate) use nzusize;
}
}
100nzusize
expands to crate::custom_literal::integer::nzusize!(100)
. it works like this:
- Inside the
proc_macro
, I obtain the string"100"
then parse it as au128
, then pass it toproc_macro::Literal::u128_unsuffixed
to generate the Rust literal100
which is directly passed tonzusize!
macro
The problem is that the Literal
API does not allow me to create a literal of a completely arbitrary size. The maximum value a literal created from a proc macro can be is u128::MAX
, but literals that are inputs to the proc macro can be of arbitrary length
Motivating examples or use cases
Suppose a user of my crate wanted to make a "bigint" literal that can be of arbitrary size, well they can't! Their big integers will be restricted to u128::MAX
, because proc macros cannot create literals that are larger than u128::MAX
Solution sketch
I propose to add new functions to proc_macro::Literal
:
impl Literal {
fn int_unsuffixed(int: &str) -> Literal { /* ... */ }
fn float_unsuffixed(float: &str) -> Literal { /* ... */ }
}
This directly creates the Literal
without any restrictions on size, so it is in a string. The integer is still validated to be a valid float/integer (and panics if it isn't), but its length can be unlimited
I think we should call them _unsuffixed
to allow for the possibility of int_suffixed
and float_suffixed
which would accept arbitrary suffix &str
. This could be useful if another crate generated code that my proc macro consumed, but it is not part of this proposal
Alternatives
Limit ourselves to emitting literals that are at most u128
big. So the APIs people can create are less powerful
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.