From ec897c20a8e59dda5a5a846a8c06f067435eace0 Mon Sep 17 00:00:00 2001 From: Christian Borst Date: Mon, 29 Apr 2024 13:18:44 -0400 Subject: [PATCH 1/2] Impl TryFrom<&str> for Uint256 and Int256 --- src/int256.rs | 8 ++++++++ src/uint256.rs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/int256.rs b/src/int256.rs index c034a44..b8149d3 100644 --- a/src/int256.rs +++ b/src/int256.rs @@ -218,6 +218,14 @@ impl FromStr for Int256 { } } +impl TryFrom<&str> for Int256 { + type Error = crate::error::ParseError; + + fn try_from(value: &str) -> Result { + value.parse() + } +} + impl TryFrom for Int256 { type Error = (); diff --git a/src/uint256.rs b/src/uint256.rs index ca09ecc..feb3de9 100644 --- a/src/uint256.rs +++ b/src/uint256.rs @@ -183,6 +183,14 @@ impl FromStr for Uint256 { } } +impl TryFrom<&str> for Uint256 { + type Error = crate::error::ParseError; + + fn try_from(value: &str) -> Result { + value.parse() + } +} + impl fmt::Display for Uint256 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", &self.0.to_str_radix(10)) From 7dc216ca8705aa123f83c1d4ca32affa6e5e96ee Mon Sep 17 00:00:00 2001 From: Christian Borst Date: Mon, 29 Apr 2024 14:31:25 -0400 Subject: [PATCH 2/2] Implement Pow for U/Int256 --- src/int256.rs | 10 +++++++++- src/uint256.rs | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/int256.rs b/src/int256.rs index b8149d3..030e0ae 100644 --- a/src/int256.rs +++ b/src/int256.rs @@ -1,7 +1,7 @@ use bnum::types::I256; use bnum::BInt; use num_traits::{ - Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Signed, + Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, Signed, ToPrimitive, Zero, }; use serde::ser::Serialize; @@ -293,6 +293,14 @@ impl Signed for Int256 { } } +impl Pow for Int256 { + type Output = Self; + + fn pow(self, p: u32) -> Self::Output { + Int256(self.0.pow(p)) + } +} + /// A macro that forwards an unary operator trait i.e. Add macro_rules! forward_op { (impl $trait_: ident for $type_: ident { fn $method: ident }) => { diff --git a/src/uint256.rs b/src/uint256.rs index feb3de9..495550b 100644 --- a/src/uint256.rs +++ b/src/uint256.rs @@ -2,8 +2,8 @@ pub use super::Int256; use bnum::types::U256; use bnum::BUint; use num_traits::{ - Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, ToPrimitive, - Zero, + Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, FromPrimitive, Num, One, Pow, + ToPrimitive, Zero, }; use serde::ser::Serialize; use serde::{Deserialize, Deserializer, Serializer}; @@ -309,6 +309,14 @@ impl Into<[u8; 32]> for Uint256 { } } +impl Pow for Uint256 { + type Output = Self; + + fn pow(self, p: u32) -> Self::Output { + Uint256(self.0.pow(p)) + } +} + macro_rules! uint_impl_from_uint { ($T:ty) => { impl From<$T> for Uint256 {