Skip to content

Commit

Permalink
Implement Pow for U/Int256
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBorst committed Apr 29, 2024
1 parent ec897c2 commit 7dc216c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 9 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 @@ -293,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
12 changes: 10 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 @@ -309,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

0 comments on commit 7dc216c

Please sign in to comment.