-
Notifications
You must be signed in to change notification settings - Fork 46
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
Comments
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 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)); |
I wrote a little demo to show this idea. It works only if You can put this demo usage of #![feature(proc_macro_hygiene)]
extern crate typenum;
type U99999999999999 = typenum::tyint!(99999999999999usize);
type P99999999999999 = typenum::tyint!(99999999999999isize); By running #![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 |
@jerry73204 you meant to link rust-lang/rust#63931 right? |
Yes, exactly. |
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. |
@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 |
@WaffleLapkin New changes are made in #136. Now it's still in PR queue. Edit: typo |
Since only types up to 1024 and power of 2 are available in
consts
mod, rather thanSum<X, Y>
combination, I'm wondering a convenient way to build types for large numbers.Suppose we could have a macro like
tyint!(9999999)
ortyuint!(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 ofSum<X, Y>
.Besides, I see const generics does the same job. There's still open issue that it panics the compiler.
The text was updated successfully, but these errors were encountered: