Skip to content

Commit

Permalink
unbreak on older Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
strake committed Dec 19, 2018
1 parent 58055fb commit f6f06f8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ fn main() {
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
panic!("i128 support was not detected!");
}

if probe("#[derive(Clone)] struct A; fn main() { let _ = [A; 2].clone(); }") {
println!("cargo:rustc-cfg=array_clone");
}
}

/// Test if a code snippet can be compiled
Expand Down
34 changes: 21 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,18 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
/// # Examples
///
/// ~~~
/// # extern crate num_integer;
/// # extern crate num_traits;
/// # fn main() {
/// # use num_integer::{ExtendedGcd, Integer};
/// # use num_traits::NumAssign;
/// fn check<A: Clone + Integer + NumAssign>(a: A, b: A) -> bool {
/// let ExtendedGcd { gcd, coeffs: [x, y], .. } = a.extended_gcd(&b);
/// gcd == x * a + y * b
/// fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
/// let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
/// gcd == coeffs[0] * a + coeffs[1] * b
/// }
/// assert!(check(10isize, 4isize));
/// assert!(check(8isize, 9isize));
/// # }
/// ~~~
#[inline]
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>
Expand Down Expand Up @@ -239,13 +242,20 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
}

/// Greatest common divisor and Bézout coefficients
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Copy, PartialEq, Eq)]
#[cfg_attr(array_clone, derive(Clone))]
pub struct ExtendedGcd<A> {
pub gcd: A,
pub coeffs: [A; 2],
_hidden: (),
}

#[cfg(not(array_clone))]
impl<A: Copy> Clone for ExtendedGcd<A> {
#[inline]
fn clone(&self) -> Self { *self }
}

/// Simultaneous integer division and modulus
#[inline]
pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) {
Expand Down Expand Up @@ -577,9 +587,8 @@ macro_rules! impl_integer_for_isize {
#[test]
fn test_gcd_lcm() {
use core::iter::once;
let r = once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128));
for i in r.clone() {
for j in r.clone() {
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
assert_eq!(i.gcd_lcm(&j), (i.gcd(&j), i.lcm(&j)));
}
}
Expand All @@ -590,15 +599,14 @@ macro_rules! impl_integer_for_isize {
use ExtendedGcd;
use traits::NumAssign;

fn check<A: Clone + Integer + NumAssign>(a: A, b: A) -> bool {
let ExtendedGcd { gcd, coeffs: [x, y], .. } = a.extended_gcd(&b);
gcd == x * a + y * b
fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
gcd == coeffs[0] * a + coeffs[1] * b
}

use core::iter::once;
let r = once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128));
for i in r.clone() {
for j in r.clone() {
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
check(i, j);
let (ExtendedGcd { gcd, .. }, lcm) = i.extended_gcd_lcm(&j);
assert_eq!((gcd, lcm), (i.gcd(&j), i.lcm(&j)));
Expand Down

0 comments on commit f6f06f8

Please sign in to comment.