Skip to content

Commit 6accdbc

Browse files
committed
unbreak on older Rust
1 parent 58055fb commit 6accdbc

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ fn main() {
88
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
99
panic!("i128 support was not detected!");
1010
}
11+
12+
if probe("#[derive(Clone)] struct A; fn main() { let _ = [A; 2].clone(); }") {
13+
println!("cargo:rustc-cfg=array_clone");
14+
}
1115
}
1216

1317
/// Test if a code snippet can be compiled

src/lib.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
120120
/// # Examples
121121
///
122122
/// ~~~
123+
/// # extern crate num_integer;
123124
/// # extern crate num_traits;
124125
/// # use num_integer::{ExtendedGcd, Integer};
125126
/// # use num_traits::NumAssign;
126-
/// fn check<A: Clone + Integer + NumAssign>(a: A, b: A) -> bool {
127-
/// let ExtendedGcd { gcd, coeffs: [x, y], .. } = a.extended_gcd(&b);
128-
/// gcd == x * a + y * b
127+
/// fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
128+
/// let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
129+
/// gcd == coeffs[0] * a + coeffs[1] * b
129130
/// }
130131
/// assert!(check(10isize, 4isize));
131132
/// assert!(check(8isize, 9isize));
@@ -239,13 +240,20 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
239240
}
240241

241242
/// Greatest common divisor and Bézout coefficients
242-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
243+
#[derive(Debug, Copy, PartialEq, Eq)]
244+
#[cfg_attr(array_clone, derive(Clone))]
243245
pub struct ExtendedGcd<A> {
244246
pub gcd: A,
245247
pub coeffs: [A; 2],
246248
_hidden: (),
247249
}
248250

251+
#[cfg(not(array_clone))]
252+
impl<A: Copy> Clone for ExtendedGcd<A> {
253+
#[inline]
254+
fn clone(&self) -> Self { *self }
255+
}
256+
249257
/// Simultaneous integer division and modulus
250258
#[inline]
251259
pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) {
@@ -577,9 +585,8 @@ macro_rules! impl_integer_for_isize {
577585
#[test]
578586
fn test_gcd_lcm() {
579587
use core::iter::once;
580-
let r = once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128));
581-
for i in r.clone() {
582-
for j in r.clone() {
588+
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
589+
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
583590
assert_eq!(i.gcd_lcm(&j), (i.gcd(&j), i.lcm(&j)));
584591
}
585592
}
@@ -590,15 +597,14 @@ macro_rules! impl_integer_for_isize {
590597
use ExtendedGcd;
591598
use traits::NumAssign;
592599

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

598605
use core::iter::once;
599-
let r = once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128));
600-
for i in r.clone() {
601-
for j in r.clone() {
606+
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
607+
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
602608
check(i, j);
603609
let (ExtendedGcd { gcd, .. }, lcm) = i.extended_gcd_lcm(&j);
604610
assert_eq!((gcd, lcm), (i.gcd(&j), i.lcm(&j)));

0 commit comments

Comments
 (0)