parity-common icon indicating copy to clipboard operation
parity-common copied to clipboard

[uint] Better constant support

Open uint opened this issue 2 years ago • 4 comments

Most ways to construct values seem not to be usable in constants except for zero() and MAX. I wonder if it would be possible/desirable to have some way to construct these things in a const context, e.g. with from_big_endian()? Or by directly providing [u64; N_WORDS]?

Maybe then this could be made more convenient with macros, e.g. a macro that converts an integer literal to [u64; N_WORDS], usable in a const context.

uint avatar Sep 02 '21 11:09 uint

It's possible to create it like

const THE_ANSWER: U256 = U256([42, 0, 0, 0]);

however, I agree a convenient method would be good.

ordian avatar Sep 02 '21 11:09 ordian

It's possible to create it like

const THE_ANSWER: U256 = U256([42, 0, 0, 0]);

Ahh, I haven't noticed the field is public. Nice.

uint avatar Sep 02 '21 11:09 uint

In https://github.com/CosmWasm/cosmwasm/pull/1071 you find a const implementation of from_be_bytes/from_le_bytes for inspiration. Getting something similar in uint directly would be 🎖

webmaster128 avatar Sep 03 '21 15:09 webmaster128

FYI https://github.com/gakonst/ethers-rs/issues/2486#issuecomment-1656893389 -- uint v0.9.3 has a few functions that could be const that aren't (e.g. fn one), whereas uint v0.9.4 has them as const (not sure if it's maximally const).

vdods avatar Jul 29 '23 21:07 vdods