Skip to content

Commit f6f06f8

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

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-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: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,18 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
120120
/// # Examples
121121
///
122122
/// ~~~
123+
/// # extern crate num_integer;
123124
/// # extern crate num_traits;
125+
/// # fn main() {
124126
/// # use num_integer::{ExtendedGcd, Integer};
125127
/// # 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
128+
/// fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
129+
/// let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
130+
/// gcd == coeffs[0] * a + coeffs[1] * b
129131
/// }
130132
/// assert!(check(10isize, 4isize));
131133
/// assert!(check(8isize, 9isize));
134+
/// # }
132135
/// ~~~
133136
#[inline]
134137
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>
@@ -239,13 +242,20 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
239242
}
240243

241244
/// Greatest common divisor and Bézout coefficients
242-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
245+
#[derive(Debug, Copy, PartialEq, Eq)]
246+
#[cfg_attr(array_clone, derive(Clone))]
243247
pub struct ExtendedGcd<A> {
244248
pub gcd: A,
245249
pub coeffs: [A; 2],
246250
_hidden: (),
247251
}
248252

253+
#[cfg(not(array_clone))]
254+
impl<A: Copy> Clone for ExtendedGcd<A> {
255+
#[inline]
256+
fn clone(&self) -> Self { *self }
257+
}
258+
249259
/// Simultaneous integer division and modulus
250260
#[inline]
251261
pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) {
@@ -577,9 +587,8 @@ macro_rules! impl_integer_for_isize {
577587
#[test]
578588
fn test_gcd_lcm() {
579589
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() {
590+
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
591+
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
583592
assert_eq!(i.gcd_lcm(&j), (i.gcd(&j), i.lcm(&j)));
584593
}
585594
}
@@ -590,15 +599,14 @@ macro_rules! impl_integer_for_isize {
590599
use ExtendedGcd;
591600
use traits::NumAssign;
592601

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
602+
fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
603+
let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
604+
gcd == coeffs[0] * a + coeffs[1] * b
596605
}
597606

598607
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() {
608+
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
609+
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
602610
check(i, j);
603611
let (ExtendedGcd { gcd, .. }, lcm) = i.extended_gcd_lcm(&j);
604612
assert_eq!((gcd, lcm), (i.gcd(&j), i.lcm(&j)));

0 commit comments

Comments
 (0)