rust-num / num-traits

Numeric traits for generic mathematics in Rust
Apache License 2.0
694 stars 131 forks source link

Feature request: can we have a trait for checked_add_signed uX implementations? #302

Open SET001 opened 9 months ago

SET001 commented 9 months ago

all uX and iX types implements checked_add_signed and it would be handy to have a trait as marker for such types:

pub trait CheckedAddSigned
where
  Self: Sized,
{
  type RHS: TryFrom<i32>;
  fn checked_add_signed(self, rhs: Self::RHS) -> Option<Self>;
}

macro_rules! impl_checked_add_signed {
  ($t:ident, $rhs:ident) => {
    impl CheckedAddSigned for $t {
      type RHS = $rhs;
      fn checked_add_signed(self, rhs: Self::RHS) -> Option<Self> {
        Self::checked_add_signed(self, rhs)
      }
    }
  };
}

impl_checked_add_signed!(u8, i8);
impl_checked_add_signed!(u16, i16);
impl_checked_add_signed!(u32, i32);
impl_checked_add_signed!(u64, i64);
impl_checked_add_signed!(usize, isize);