-
Notifications
You must be signed in to change notification settings - Fork 220
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
uint: add casting from/to U64 #691
Open
halo3mic
wants to merge
2
commits into
paritytech:master
Choose a base branch
from
halo3mic:cast_u64
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−23
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use primitive_types::{Error, U128, U256, U512, U64}; | ||
|
||
|
||
#[test] | ||
#[allow(non_snake_case)] | ||
fn from_U64() { | ||
let a = U64::from(222); | ||
|
||
assert_eq!(U128::from(a), U128::from(222), "U64 -> U128"); | ||
assert_eq!(U256::from(a), U256::from(222), "U64 -> U256"); | ||
assert_eq!(U512::from(a), U512::from(222), "U64 -> U512"); | ||
} | ||
|
||
#[test] | ||
#[allow(non_snake_case)] | ||
fn to_U64() { | ||
// U128 -> U64 | ||
assert_eq!(U64::try_from(U128([222, 0])), Ok(U64::from(222)), "U128 -> U64"); | ||
assert_eq!(U64::try_from(U128([222, 1])), Err(Error::Overflow), "U128 -> U64 :: Overflow"); | ||
|
||
// U256 -> U64 | ||
assert_eq!(U64::try_from(U256([222, 0, 0, 0])), Ok(U64::from(222)), "U256 -> U64"); | ||
for i in 1..4 { | ||
let mut arr = [222, 0, 0, 0]; | ||
arr[i] = 1; | ||
assert_eq!(U64::try_from(U256(arr)), Err(Error::Overflow), "U256 -> U64 :: Overflow"); | ||
} | ||
|
||
// U512 -> U64 | ||
assert_eq!(U64::try_from(U512([222, 0, 0, 0, 0, 0, 0, 0])),Ok(U64::from(222)), "U512 -> U64"); | ||
for i in 1..8 { | ||
let mut arr = [222, 0, 0, 0, 0, 0, 0, 0]; | ||
arr[i] = 1; | ||
assert_eq!(U64::try_from(U512(arr)), Err(Error::Overflow), "U512 -> U64"); | ||
} | ||
} | ||
|
||
#[test] | ||
#[allow(non_snake_case)] | ||
fn full_mul_U64() { | ||
let a = U64::MAX; | ||
let b = U64::from(2); | ||
assert_eq!(a.full_mul(b), U128::from(a)*U128::from(b)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not keen on adding this type to primitives.
In fact, we wanted to remove it altogether, see #473 for the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad for not reading this issue thoroughly, and thanks for pointing it out.
I understand now why you suggested adding this to
ethereum-types
a year ago in #473. Just for clarity, you are still okay with adding this functionality in general, right?My motivation behind this is that there are still libraries that use U64 in certain cases (eg. ethers-rs block-num) and conversion to other uint types is currently not the most elegant as already pointed out in the referenced issue.