Skip to content

Commit

Permalink
define and implement the Incrementable trait for uints
Browse files Browse the repository at this point in the history
  • Loading branch information
Moliholy committed Apr 2, 2024
1 parent d5e9c1d commit 2266572
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion primitive-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use core::convert::TryFrom;
use fixed_hash::{construct_fixed_hash, impl_fixed_hash_conversions};
#[cfg(feature = "scale-info")]
use scale_info_crate::TypeInfo;
use uint::{construct_uint, uint_full_mul_reg};
use uint::{construct_uint, uint_full_mul_reg, Incrementable};

/// Error type for conversion.
#[derive(Debug, PartialEq, Eq)]
Expand Down
1 change: 1 addition & 0 deletions uint/examples/modular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#[macro_use]
extern crate uint;
use uint::Incrementable;

construct_uint! {
pub struct U256(4);
Expand Down
29 changes: 29 additions & 0 deletions uint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ impl From<FromHexError> for FromStrRadixErr {
}
}

/// A trait representing an incrementable type.
///
/// The `increment` and `initial_value` functions are fallible.
/// They should either both return `Some` with a valid value, or `None`.
pub trait Incrementable
where
Self: Sized,
{
/// Increments the value.
///
/// Returns `Some` with the incremented value if it is possible, or `None` if it is not.
fn increment(&self) -> Option<Self>;

/// Returns the initial value.
///
/// Returns `Some` with the initial value if it is available, or `None` if it is not.
fn initial_value() -> Option<Self>;
}

/// Conversion from decimal string error
#[derive(Debug, PartialEq, Eq)]
pub enum FromDecStrErr {
Expand Down Expand Up @@ -493,6 +512,16 @@ macro_rules! construct_uint {
}
}

impl Incrementable for $name {
fn increment(&self) -> Option<Self> {
self.checked_add(Self::one())
}

fn initial_value() -> Option<Self> {
Some(Self::zero())
}
}

impl $name {
/// Low 2 words (u128)
#[inline]
Expand Down
15 changes: 13 additions & 2 deletions uint/tests/uint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use core::{convert::TryInto, str::FromStr, u64::MAX};
use crunchy::unroll;
use uint::{construct_uint, overflowing, FromDecStrErr};
use uint::{construct_uint, overflowing, FromDecStrErr, Incrementable};

construct_uint! {
pub struct U256(4);
Expand Down Expand Up @@ -1192,9 +1192,20 @@ fn bit_assign() {
check(U256::MAX, U256::zero());
}

#[test]
fn increment() {
assert_eq!(U256::from(0).increment(), Some(1.into()));
assert_eq!(U256::max_value().increment(), None);
assert_eq!(U256::initial_value(), Some(0.into()));

assert_eq!(U512::from(0).increment(), Some(1.into()));
assert_eq!(U512::max_value().increment(), None);
assert_eq!(U512::initial_value(), Some(0.into()));
}

#[cfg(feature = "quickcheck")]
pub mod laws {
use super::construct_uint;
use super::{construct_uint, Incrementable};
macro_rules! uint_laws {
($mod_name:ident, $uint_ty:ident) => {
mod $mod_name {
Expand Down

0 comments on commit 2266572

Please sign in to comment.