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

Impl Pow + TryFrom<&str> for Uint256 and Int256 #23

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/int256.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -218,6 +218,14 @@ impl FromStr for Int256 {
}
}

impl TryFrom<&str> for Int256 {
type Error = crate::error::ParseError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}

impl TryFrom<Uint256> for Int256 {
type Error = ();

Expand Down Expand Up @@ -285,6 +293,14 @@ impl Signed for Int256 {
}
}

impl Pow<u32> 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 }) => {
Expand Down
20 changes: 18 additions & 2 deletions src/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -183,6 +183,14 @@ impl FromStr for Uint256 {
}
}

impl TryFrom<&str> for Uint256 {
type Error = crate::error::ParseError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
value.parse()
}
}

impl fmt::Display for Uint256 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", &self.0.to_str_radix(10))
Expand Down Expand Up @@ -301,6 +309,14 @@ impl Into<[u8; 32]> for Uint256 {
}
}

impl Pow<u32> 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 {
Expand Down
Loading