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

Add readable constructors via proc-macro. #33

Merged

Conversation

chaoticlonghair
Copy link
Contributor

@chaoticlonghair chaoticlonghair commented Mar 26, 2019

Before this PR, if u want to construct a fixed uint / hash in compile time (not runtime), u have to write code like:

const X: U128 = U128([0x64, 0]);
let y = U256([0x6bc7_5e2d_6310_0000, 0x5, 0, 0]);
// In fact, rustfmt will put each `0` in a separate line, more ugly than this example
let z = U4096([
    0x00ab_cdef, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0,
]);

const H: H256 = H256([
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff,
]);

But after this PR, u can write code like these examples:

const X: U128 = u128!("100");
let y = u256!("100_000_000_000_000_000_000");
let z = u4096!("0xabcdef");

const H: H4096 = h4096!("0x_ffff_ffff_ffff_ffff");

The function from_bin_str, from_oct_str, from_hex_str, from_dec_str, hash::from_str, hash::from_hex_str, hash::from_trimmed_hex_str and other similar methods will construct variables in runtime. That will be slow and unsafe (panic in compile time is better than throw exceptions in runtime).

But if use there macros, the variables will be constructed in compile time.

Close #4.

let y = U4096([
0x00ab_cdef,
0,
0,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😢

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is why I push this PR. 😎.

@chaoticlonghair chaoticlonghair merged commit 6ebdd0e into cryptape:develop Mar 27, 2019
@chaoticlonghair chaoticlonghair deleted the readable-constructor branch April 3, 2019 07:44
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.

Need macros to construct structs
4 participants