Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishdaulatabad committed Apr 4, 2024
1 parent 8c282b7 commit d25319b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/u256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ impl U256 {
Self(value)
}

/// Create raw value of unsigned integer
pub(super) fn get_raw(self) -> [u64; 4] {
self.0
}

#[inline(always)]
pub const fn raw_eq(self, other: [u64; 4]) -> bool {
self.0[0] == other[0]
Expand Down
37 changes: 25 additions & 12 deletions src/u512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,14 @@ impl core::str::FromStr for U512 {
}
}

impl From<super::u256::U256> for U512 {
#[inline]
fn from(value: super::u256::U256) -> Self {
let val = value.get_raw();
Self([0, 0, 0, 0, val[0], val[1], val[2], val[3]])
}
}

impl From<u128> for U512 {
#[inline]
fn from(value: u128) -> Self {
Expand Down Expand Up @@ -1184,6 +1192,13 @@ impl From<U512> for u128 {
}
}

impl From<U512> for super::u256::U256 {
#[inline]
fn from(value: U512) -> Self {
Self::raw([value.0[4], value.0[5], value.0[6], value.0[7]])
}
}

impl Into<u64> for U512 {

Check warning on line 1202 in src/u512.rs

View workflow job for this annotation

GitHub Actions / clippy

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true

warning: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true --> src/u512.rs:1202:1 | 1202 | impl Into<u64> for U512 { | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: `impl From<Local> for Foreign` is allowed by the orphan rules, for more information see https://doc.rust-lang.org/reference/items/implementations.html#trait-implementation-coherence = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_over_into help: replace the `Into` implementation with `From<u512::U512>` | 1202 ~ impl From<U512> for u64 { 1203 | #[inline] 1204 ~ fn from(val: U512) -> Self { 1205 ~ val.0[7] |
#[inline]
fn into(self) -> u64 {
Expand Down Expand Up @@ -1591,8 +1606,8 @@ mod test {
// Equal to 115792089237316195423570985008687907852837564279074904382605163141518161494337
let value = U512::from_string("115792089237316195423570985008687907852837564279074904382605163141518161494337")?;
assert_eq!(
value.0,
[
value,
U512([
0,
0,
0,
Expand All @@ -1601,13 +1616,13 @@ mod test {
18446744073709551614,
13451932020343611451,
13822214165235122497
]
])
);

let value = U512::from_string("16983810465656793445178183341822322175883642221536626637512293983324")?;
assert_eq!(
value.0,
[
value,
U512([
0,
0,
0,
Expand All @@ -1616,14 +1631,14 @@ mod test {
0x4df099df30fc28a1,
0x69a467e9e47075a9,
0x0f7e650eb6b7a45c
]
])
);

// Max value of 256-bit number
let value = U512::from_string("115792089237316195423570985008687907853269984665640564039457584007913129639935")?;
assert_eq!(
value.0,
[
value,
U512([
0,
0,
0,
Expand All @@ -1632,7 +1647,7 @@ mod test {
0xFFFF_FFFF_FFFF_FFFF,
0xFFFF_FFFF_FFFF_FFFF,
0xFFFF_FFFF_FFFF_FFFF,
]
])
);

// Max value of 512-bit number
Expand Down Expand Up @@ -1760,9 +1775,7 @@ mod test {

assert_eq!(
c,
U512::from_string(
"6270385922947262222347954536162455298520515727022267860678267425509717888"
)?
"6270385922947262222347954536162455298520515727022267860678267425509717888".parse::<U512>()?
);

Ok(())
Expand Down

0 comments on commit d25319b

Please sign in to comment.