Skip to content

Commit

Permalink
Added From<bool> for BigInt and BigUint
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickNorton committed Apr 16, 2022
1 parent e77ffac commit 6858af0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/bigint/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use core::cmp::Ordering::{Equal, Greater, Less};
#[cfg(has_try_from)]
use core::convert::TryFrom;
use core::str::{self, FromStr};
use num_traits::{FromPrimitive, Num, ToPrimitive, Zero};
use num_traits::{FromPrimitive, Num, One, ToPrimitive, Zero};

impl FromStr for BigInt {
type Err = ParseBigIntError;
Expand Down Expand Up @@ -367,6 +367,16 @@ impl_to_bigint!(u128, FromPrimitive::from_u128);
impl_to_bigint!(f32, FromPrimitive::from_f32);
impl_to_bigint!(f64, FromPrimitive::from_f64);

impl From<bool> for BigInt {
fn from(x: bool) -> Self {
if x {
One::one()
} else {
Zero::zero()
}
}
}

#[inline]
pub(super) fn from_signed_bytes_be(digits: &[u8]) -> BigInt {
let sign = match digits.first() {
Expand Down
12 changes: 11 additions & 1 deletion src/biguint/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use core::mem;
use core::str::FromStr;
use num_integer::{Integer, Roots};
use num_traits::float::FloatCore;
use num_traits::{FromPrimitive, Num, PrimInt, ToPrimitive, Zero};
use num_traits::{FromPrimitive, Num, One, PrimInt, ToPrimitive, Zero};

/// Find last set bit
/// fls(0) == 0, fls(u32::MAX) == 32
Expand Down Expand Up @@ -572,6 +572,16 @@ impl_to_biguint!(u128, FromPrimitive::from_u128);
impl_to_biguint!(f32, FromPrimitive::from_f32);
impl_to_biguint!(f64, FromPrimitive::from_f64);

impl From<bool> for BigUint {
fn from(x: bool) -> Self {
if x {
One::one()
} else {
Zero::zero()
}
}
}

// Extract bitwise digits that evenly divide BigDigit
pub(super) fn to_bitwise_digits_le(u: &BigUint, bits: u8) -> Vec<u8> {
debug_assert!(!u.is_zero() && bits <= 8 && big_digit::BITS % bits == 0);
Expand Down

0 comments on commit 6858af0

Please sign in to comment.