Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build typenum from integer literal #131

Open
jerry73204 opened this issue Sep 9, 2019 · 7 comments
Open

Build typenum from integer literal #131

jerry73204 opened this issue Sep 9, 2019 · 7 comments

Comments

@jerry73204
Copy link
Contributor

Since only types up to 1024 and power of 2 are available in consts mod, rather than Sum<X, Y> combination, I'm wondering a convenient way to build types for large numbers.

Suppose we could have a macro like tyint!(9999999) or tyuint!(99999999)? The Rust language already has function-like procedural macros. We can make it possible by decomposing numbers into powers of 2, and turn it in into combinations of Sum<X, Y>.

Besides, I see const generics does the same job. There's still open issue that it panics the compiler.

@paholg
Copy link
Owner

paholg commented Sep 9, 2019

I am unlikely to build it, but I would be happy with such a PR if someone else wants to.

FWIW, you can do it a bit more conveniently than using Sum<X, Y> by using the op! macro.

Ex:

type U9999999 = op!(U9 * (pow(U10, U6) + pow(U10, U5) + pow(U10, U4) + pow(U10, U3) + pow(U10, U2) + pow(U10, U1) + pow(U10, U0));

@jerry73204
Copy link
Contributor Author

I wrote a little demo to show this idea. It works only if #![feature(proc_macro_hygiene)] unstable feature is present. That's because procedural macro is not to allowed at type positions for now (rust-lang/rust#54727). Fortunately, the feature is on the track of stabilization (rust-lang/rust#54727). We can keep it on hold, or make it an optional feature.

You can put this demo usage of tyint!() macro in tests.

#![feature(proc_macro_hygiene)]
extern crate typenum;

type U99999999999999 = typenum::tyint!(99999999999999usize);
type P99999999999999 = typenum::tyint!(99999999999999isize);

By running cargo expand --test TEST_NAME, you can see the expansion.

#![feature(prelude_import)]
#![no_std]
#![feature(proc_macro_hygiene)]
#[prelude_import]
use ::std::prelude::v1::*;
#[macro_use]
extern crate std;
extern crate typenum;
type U99999999999999 = typenum :: Sum < typenum :: consts :: U70368744177664 , typenum :: Sum < typenum :: consts :: U17592186044416 , typenum :: Sum < typenum :: consts :: U8796093022208 , typenum :: Sum < typenum :: consts :: U2199023255552 , typenum :: Sum < typenum :: consts :: U549755813888 , typenum :: Sum < typenum :: consts :: U274877906944 , typenum :: Sum < typenum :: consts :: U137438953472 , typenum :: Sum < typenum :: consts :: U68719476736 , typenum :: Sum < typenum :: consts :: U8589934592 , typenum :: Sum < typenum :: consts :: U4294967296 , typenum :: Sum < typenum :: consts :: U268435456 , typenum :: Sum < typenum :: consts :: U4194304 , typenum :: Sum < typenum :: consts :: U2097152 , typenum :: Sum < typenum :: consts :: U1048576 , typenum :: Sum < typenum :: consts :: U524288 , typenum :: Sum < typenum :: consts :: U131072 , typenum :: Sum < typenum :: consts :: U8192 , typenum :: Sum < typenum :: consts :: U4096 , typenum :: Sum < typenum :: consts :: U2048 , typenum :: Sum < typenum :: consts :: U1024 , typenum :: Sum < typenum :: consts :: U512 , typenum :: Sum < typenum :: consts :: U256 , typenum :: Sum < typenum :: consts :: U128 , typenum :: Sum < typenum :: consts :: U64 , typenum :: Sum < typenum :: consts :: U32 , typenum :: Sum < typenum :: consts :: U16 , typenum :: Sum < typenum :: consts :: U8 , typenum :: Sum < typenum :: consts :: U4 , typenum :: Sum < typenum :: consts :: U2 , typenum :: Sum < typenum :: consts :: U1 , typenum :: consts :: U0 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ;
type P99999999999999 = typenum :: Sum < typenum :: consts :: P70368744177664 , typenum :: Sum < typenum :: consts :: P17592186044416 , typenum :: Sum < typenum :: consts :: P8796093022208 , typenum :: Sum < typenum :: consts :: P2199023255552 , typenum :: Sum < typenum :: consts :: P549755813888 , typenum :: Sum < typenum :: consts :: P274877906944 , typenum :: Sum < typenum :: consts :: P137438953472 , typenum :: Sum < typenum :: consts :: P68719476736 , typenum :: Sum < typenum :: consts :: P8589934592 , typenum :: Sum < typenum :: consts :: P4294967296 , typenum :: Sum < typenum :: consts :: P268435456 , typenum :: Sum < typenum :: consts :: P4194304 , typenum :: Sum < typenum :: consts :: P2097152 , typenum :: Sum < typenum :: consts :: P1048576 , typenum :: Sum < typenum :: consts :: P524288 , typenum :: Sum < typenum :: consts :: P131072 , typenum :: Sum < typenum :: consts :: P8192 , typenum :: Sum < typenum :: consts :: P4096 , typenum :: Sum < typenum :: consts :: P2048 , typenum :: Sum < typenum :: consts :: P1024 , typenum :: Sum < typenum :: consts :: P512 , typenum :: Sum < typenum :: consts :: P256 , typenum :: Sum < typenum :: consts :: P128 , typenum :: Sum < typenum :: consts :: P64 , typenum :: Sum < typenum :: consts :: P32 , typenum :: Sum < typenum :: consts :: P16 , typenum :: Sum < typenum :: consts :: P8 , typenum :: Sum < typenum :: consts :: P4 , typenum :: Sum < typenum :: consts :: P2 , typenum :: Sum < typenum :: consts :: P1 , typenum :: consts :: Z0 > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ;

Note that negative numbers tyint!(-314159) are not understood by the macro. I think minor change can fix this.

@est31
Copy link

est31 commented Sep 12, 2019

@jerry73204 you meant to link rust-lang/rust#63931 right?

@jerry73204
Copy link
Contributor Author

Yes, exactly.

@dtolnay
Copy link

dtolnay commented Oct 2, 2019

If you want this to work earlier than the expression-position proc macro stabilization, it should be pretty easy to make it work with proc-macro-hack.

@WaffleLapkin
Copy link
Contributor

@jerry73204 it seems like proc-macro in type position got stabilized 1 October 2019 — half a year ago. Do you plan to update your demo / open a PR with tyint! macro to this repo?

@jerry73204
Copy link
Contributor Author

jerry73204 commented Sep 14, 2020

@WaffleLapkin New changes are made in #136. Now it's still in PR queue.

Edit: typo

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

No branches or pull requests

5 participants