Skip to content

Add PrimitiveNumber::CONST for 0..=127#26

Open
cuviper wants to merge 2 commits intorust-num:mainfrom
cuviper:consts
Open

Add PrimitiveNumber::CONST for 0..=127#26
cuviper wants to merge 2 commits intorust-num:mainfrom
cuviper:consts

Conversation

@cuviper
Copy link
Copy Markdown
Member

@cuviper cuviper commented Mar 17, 2026

These are all of the numbers common to every primitive numeric type,
available in constant form. Each value corresponds to its usize index.

This could be seen as a replacement and extension of ConstZero::ZERO
and ConstOne::ONE from num-traits, without worrying about the
particulars of being additive/multiplicative identities. Here, we're
just describing this as a workaround for the lack of generic literals.

@cuviper
Copy link
Copy Markdown
Member Author

cuviper commented Mar 17, 2026

Naming is hard -- this might be better with a longer name like CONSTANTS, SMALL_VALUES, etc. But I think in use, examples like T::CONST[0] and T::CONST[42] are nice and succinct.

@cuviper cuviper force-pushed the consts branch 2 times, most recently from d12c7de to daf0e32 Compare March 17, 2026 18:45
These are all of the numbers common to every primitive numeric type,
available in constant form. Each value corresponds to its `usize` index.

This could be seen as a replacement and extension of `ConstZero::ZERO`
and `ConstOne::ONE` from `num-traits`, without worrying about the
particulars of being additive/multiplicative identities. Here, we're
just describing this as a workaround for the lack of generic literals.
@cuviper
Copy link
Copy Markdown
Member Author

cuviper commented Mar 18, 2026

I'm having second thoughts about this, because the array access doesn't get optimized in the front end like I hoped. In the milu example I put in the docs, with 3 CONST accesses, the LLVM IR ends up with 3 full alloca+memcpy for the whole array, even in release mode (pre-optimization). That does ultimately get optimized out in release mode, but every memcpy remains in the final output in debug mode.

It's better when that's forced to compile time, e.g. const { T::CONST[_] }, but that's more cumbersome. I might see if we can make that easier with a macro, perhaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant