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

From<i64> impl is invalid #764

Open
benluelo opened this issue Jul 7, 2023 · 2 comments
Open

From<i64> impl is invalid #764

benluelo opened this issue Jul 7, 2023 · 2 comments

Comments

@benluelo
Copy link

benluelo commented Jul 7, 2023

This implementation is invalid:

parity-common/uint/src/uint.rs

Lines 1349 to 1356 in a5ef730

impl $crate::core_::convert::From<i64> for $name {
fn from(value: i64) -> $name {
match value >= 0 {
true => From::from(value as u64),
false => { panic!("Unsigned integer can't be created from negative value"); }
}
}
}

As per the docs on From:

Note: This trait must not fail. The From trait is intended for perfect conversions. If the conversion can fail or is not perfect, use TryFrom.

@benluelo
Copy link
Author

benluelo commented Jul 7, 2023

From<i128> is also invalid:

impl $crate::core_::convert::From<i128> for $name {
fn from(value: i128) -> $name {
match value >= 0 {
true => From::from(value as u128),
false => { panic!("Unsigned integer can't be created from negative value"); }
}
}
}

@ordian
Copy link
Member

ordian commented Jul 7, 2023

This initial impl predates the stabilization of TryFrom trait and should be replaced by that (breaking change) indeed nowadays.

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

No branches or pull requests

2 participants