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

U types do not convert to each other #568

Open
rakanalh opened this issue Jul 13, 2021 · 2 comments · May be fixed by #691
Open

U types do not convert to each other #568

rakanalh opened this issue Jul 13, 2021 · 2 comments · May be fixed by #691

Comments

@rakanalh
Copy link

rakanalh commented Jul 13, 2021

Hello,

Is there a reason why U* types do not implement From/Into from each other? At least to be able to cast upwards.

Example,

impl From<U64> for U128 {
    fn from(num: U64) -> Self {
        num.low_u64().into() // U128 implements From<u64>
    }
}

impl From<U64> for U256 {
    fn from(num: U64) -> Self {
        num.low_u64().into() // U256 implements From<u64>
    }
}
@ordian
Copy link
Member

ordian commented Jul 13, 2021

they do in primitive-types

impl From<U256> for U512 {
fn from(value: U256) -> U512 {
let U256(ref arr) = value;
let mut ret = [0; 8];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
U512(ret)
}
}
impl TryFrom<U256> for U128 {
type Error = Error;
fn try_from(value: U256) -> Result<U128, Error> {
let U256(ref arr) = value;
if arr[2] | arr[3] != 0 {
return Err(Error::Overflow);
}
let mut ret = [0; 2];
ret[0] = arr[0];
ret[1] = arr[1];
Ok(U128(ret))
}
}
impl TryFrom<U512> for U256 {
type Error = Error;
fn try_from(value: U512) -> Result<U256, Error> {
let U512(ref arr) = value;
if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
return Err(Error::Overflow);
}
let mut ret = [0; 4];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
Ok(U256(ret))
}
}
impl TryFrom<U512> for U128 {
type Error = Error;
fn try_from(value: U512) -> Result<U128, Error> {
let U512(ref arr) = value;
if arr[2] | arr[3] | arr[4] | arr[5] | arr[6] | arr[7] != 0 {
return Err(Error::Overflow);
}
let mut ret = [0; 2];
ret[0] = arr[0];
ret[1] = arr[1];
Ok(U128(ret))
}
}
impl From<U128> for U512 {
fn from(value: U128) -> U512 {
let U128(ref arr) = value;
let mut ret = [0; 8];
ret[0] = arr[0];
ret[1] = arr[1];
U512(ret)
}
}
impl From<U128> for U256 {
fn from(value: U128) -> U256 {
let U128(ref arr) = value;
let mut ret = [0; 4];
ret[0] = arr[0];
ret[1] = arr[1];
U256(ret)
}
}
impl<'a> From<&'a U256> for U512 {
fn from(value: &'a U256) -> U512 {
let U256(ref arr) = *value;
let mut ret = [0; 8];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
U512(ret)
}
}
impl<'a> TryFrom<&'a U512> for U256 {
type Error = Error;
fn try_from(value: &'a U512) -> Result<U256, Error> {
let U512(ref arr) = *value;
if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
return Err(Error::Overflow);
}
let mut ret = [0; 4];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
Ok(U256(ret))
}
}

anything missing?

@ordian
Copy link
Member

ordian commented Jul 13, 2021

U64 is kind of legacy, see #473
but we can probably add conversions from it in ethereum-types

@halo3mic halo3mic linked a pull request Nov 7, 2022 that will close this issue
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 a pull request may close this issue.

2 participants