Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
feat: implement the Zero and One traits
Browse files Browse the repository at this point in the history
This is useful for the num-iter iterators.
  • Loading branch information
bgeron committed Mar 8, 2020
1 parent 56ad682 commit 7dc4b81
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ yet. Patches are welcome!

- Bit operations
- `num_traits::Num` (easy?)
- `num_traits::One` (easy?)
- `num_traits::Signed`
- `num_traits::Unsigned`
- `num_integer::Integer`
Expand Down
36 changes: 35 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
//!
//! - Bit operations
//! - [`num_traits::Num`] (easy?)
//! - [`num_traits::One`] (easy?)
//! - [`num_traits::Signed`]
//! - [`num_traits::Unsigned`]
//! - [`num_integer::Integer`]
Expand All @@ -38,6 +37,7 @@
use either::{Either, Left, Right};
use num_bigint::{BigInt, BigUint, ParseBigIntError, ToBigInt, ToBigUint};
use num_traits::cast::{FromPrimitive, ToPrimitive};
use num_traits::identities::{One, Zero};
#[allow(unused_imports)]
use num_traits::{
CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub,
Expand Down Expand Up @@ -221,6 +221,40 @@ impl Default for Int {
}
}

impl Zero for Uint {
fn zero() -> Self {
Self::small(0)
}
fn is_zero(&self) -> bool {
*self == Self::zero()
}
}
impl Zero for Int {
fn zero() -> Self {
Self::small(0)
}
fn is_zero(&self) -> bool {
*self == Self::zero()
}
}

impl One for Uint {
fn one() -> Self {
Self::small(1)
}
fn is_one(&self) -> bool {
*self == Self::one()
}
}
impl One for Int {
fn one() -> Self {
Self::small(1)
}
fn is_one(&self) -> bool {
*self == Self::one()
}
}

/// A utility extension function to make it easier to transform values in-place. Works
/// well automatically for types with a cheap [`Default`].
trait Transformable: Sized {
Expand Down

0 comments on commit 7dc4b81

Please sign in to comment.