Skip to content

Commit 9af254a

Browse files
bors[bot]vkscuviper
authored
Merge #92
92: Deprecate `Rational` r=cuviper a=vks Fixes #89. Co-authored-by: Vinzent Steinberg <Vinzent.Steinberg@gmail.com> Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents f61b878 + 0c5b07f commit 9af254a

File tree

1 file changed

+63
-59
lines changed

1 file changed

+63
-59
lines changed

src/lib.rs

+63-59
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ pub struct Ratio<T> {
5757
}
5858

5959
/// Alias for a `Ratio` of machine-sized integers.
60+
#[deprecated(
61+
since = "0.4.0",
62+
note = "it's better to use a specific size, like `Rational32` or `Rational64`"
63+
)]
6064
pub type Rational = Ratio<isize>;
6165
/// Alias for a `Ratio` of 32-bit-sized integers.
6266
pub type Rational32 = Ratio<i32>;
@@ -1617,100 +1621,100 @@ mod test {
16171621
use super::BigInt;
16181622
#[cfg(feature = "num-bigint")]
16191623
use super::BigRational;
1620-
use super::{Ratio, Rational, Rational64};
1624+
use super::{Ratio, Rational64};
16211625

16221626
use core::f64;
16231627
use core::i32;
1624-
use core::isize;
1628+
use core::i64;
16251629
use core::str::FromStr;
16261630
use num_integer::Integer;
16271631
use num_traits::ToPrimitive;
16281632
use num_traits::{FromPrimitive, One, Pow, Signed, Zero};
16291633

1630-
pub const _0: Rational = Ratio { numer: 0, denom: 1 };
1631-
pub const _1: Rational = Ratio { numer: 1, denom: 1 };
1632-
pub const _2: Rational = Ratio { numer: 2, denom: 1 };
1633-
pub const _NEG2: Rational = Ratio {
1634+
pub const _0: Rational64 = Ratio { numer: 0, denom: 1 };
1635+
pub const _1: Rational64 = Ratio { numer: 1, denom: 1 };
1636+
pub const _2: Rational64 = Ratio { numer: 2, denom: 1 };
1637+
pub const _NEG2: Rational64 = Ratio {
16341638
numer: -2,
16351639
denom: 1,
16361640
};
1637-
pub const _8: Rational = Ratio { numer: 8, denom: 1 };
1638-
pub const _15: Rational = Ratio {
1641+
pub const _8: Rational64 = Ratio { numer: 8, denom: 1 };
1642+
pub const _15: Rational64 = Ratio {
16391643
numer: 15,
16401644
denom: 1,
16411645
};
1642-
pub const _16: Rational = Ratio {
1646+
pub const _16: Rational64 = Ratio {
16431647
numer: 16,
16441648
denom: 1,
16451649
};
16461650

1647-
pub const _1_2: Rational = Ratio { numer: 1, denom: 2 };
1648-
pub const _1_8: Rational = Ratio { numer: 1, denom: 8 };
1649-
pub const _1_15: Rational = Ratio {
1651+
pub const _1_2: Rational64 = Ratio { numer: 1, denom: 2 };
1652+
pub const _1_8: Rational64 = Ratio { numer: 1, denom: 8 };
1653+
pub const _1_15: Rational64 = Ratio {
16501654
numer: 1,
16511655
denom: 15,
16521656
};
1653-
pub const _1_16: Rational = Ratio {
1657+
pub const _1_16: Rational64 = Ratio {
16541658
numer: 1,
16551659
denom: 16,
16561660
};
1657-
pub const _3_2: Rational = Ratio { numer: 3, denom: 2 };
1658-
pub const _5_2: Rational = Ratio { numer: 5, denom: 2 };
1659-
pub const _NEG1_2: Rational = Ratio {
1661+
pub const _3_2: Rational64 = Ratio { numer: 3, denom: 2 };
1662+
pub const _5_2: Rational64 = Ratio { numer: 5, denom: 2 };
1663+
pub const _NEG1_2: Rational64 = Ratio {
16601664
numer: -1,
16611665
denom: 2,
16621666
};
1663-
pub const _1_NEG2: Rational = Ratio {
1667+
pub const _1_NEG2: Rational64 = Ratio {
16641668
numer: 1,
16651669
denom: -2,
16661670
};
1667-
pub const _NEG1_NEG2: Rational = Ratio {
1671+
pub const _NEG1_NEG2: Rational64 = Ratio {
16681672
numer: -1,
16691673
denom: -2,
16701674
};
1671-
pub const _1_3: Rational = Ratio { numer: 1, denom: 3 };
1672-
pub const _NEG1_3: Rational = Ratio {
1675+
pub const _1_3: Rational64 = Ratio { numer: 1, denom: 3 };
1676+
pub const _NEG1_3: Rational64 = Ratio {
16731677
numer: -1,
16741678
denom: 3,
16751679
};
1676-
pub const _2_3: Rational = Ratio { numer: 2, denom: 3 };
1677-
pub const _NEG2_3: Rational = Ratio {
1680+
pub const _2_3: Rational64 = Ratio { numer: 2, denom: 3 };
1681+
pub const _NEG2_3: Rational64 = Ratio {
16781682
numer: -2,
16791683
denom: 3,
16801684
};
1681-
pub const _MIN: Rational = Ratio {
1682-
numer: isize::MIN,
1685+
pub const _MIN: Rational64 = Ratio {
1686+
numer: i64::MIN,
16831687
denom: 1,
16841688
};
1685-
pub const _MIN_P1: Rational = Ratio {
1686-
numer: isize::MIN + 1,
1689+
pub const _MIN_P1: Rational64 = Ratio {
1690+
numer: i64::MIN + 1,
16871691
denom: 1,
16881692
};
1689-
pub const _MAX: Rational = Ratio {
1690-
numer: isize::MAX,
1693+
pub const _MAX: Rational64 = Ratio {
1694+
numer: i64::MAX,
16911695
denom: 1,
16921696
};
1693-
pub const _MAX_M1: Rational = Ratio {
1694-
numer: isize::MAX - 1,
1697+
pub const _MAX_M1: Rational64 = Ratio {
1698+
numer: i64::MAX - 1,
16951699
denom: 1,
16961700
};
1697-
pub const _BILLION: Rational = Ratio {
1701+
pub const _BILLION: Rational64 = Ratio {
16981702
numer: 1_000_000_000,
16991703
denom: 1,
17001704
};
17011705

17021706
#[cfg(feature = "num-bigint")]
1703-
pub fn to_big(n: Rational) -> BigRational {
1707+
pub fn to_big(n: Rational64) -> BigRational {
17041708
Ratio::new(
1705-
FromPrimitive::from_isize(n.numer).unwrap(),
1706-
FromPrimitive::from_isize(n.denom).unwrap(),
1709+
FromPrimitive::from_i64(n.numer).unwrap(),
1710+
FromPrimitive::from_i64(n.denom).unwrap(),
17071711
)
17081712
}
17091713
#[cfg(not(feature = "num-bigint"))]
1710-
pub fn to_big(n: Rational) -> Rational {
1714+
pub fn to_big(n: Rational64) -> Rational64 {
17111715
Ratio::new(
1712-
FromPrimitive::from_isize(n.numer).unwrap(),
1713-
FromPrimitive::from_isize(n.denom).unwrap(),
1716+
FromPrimitive::from_i64(n.numer).unwrap(),
1717+
FromPrimitive::from_i64(n.denom).unwrap(),
17141718
)
17151719
}
17161720

@@ -1794,7 +1798,7 @@ mod test {
17941798
assert!(_0 >= _0 && _1 >= _1);
17951799
assert!(_1 >= _0 && !(_0 >= _1));
17961800

1797-
let _0_2: Rational = Ratio::new_raw(0, 2);
1801+
let _0_2: Rational64 = Ratio::new_raw(0, 2);
17981802
assert_eq!(_0, _0_2);
17991803
}
18001804

@@ -2038,15 +2042,15 @@ mod test {
20382042
}
20392043

20402044
mod arith {
2041-
use super::super::{Ratio, Rational};
2045+
use super::super::{Ratio, Rational64};
20422046
use super::{to_big, _0, _1, _1_2, _2, _3_2, _5_2, _MAX, _MAX_M1, _MIN, _MIN_P1, _NEG1_2};
20432047
use core::fmt::Debug;
20442048
use num_integer::Integer;
20452049
use num_traits::{Bounded, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, NumAssign};
20462050

20472051
#[test]
20482052
fn test_add() {
2049-
fn test(a: Rational, b: Rational, c: Rational) {
2053+
fn test(a: Rational64, b: Rational64, c: Rational64) {
20502054
assert_eq!(a + b, c);
20512055
assert_eq!(
20522056
{
@@ -2060,7 +2064,7 @@ mod test {
20602064
assert_eq!(a.checked_add(&b), Some(c));
20612065
assert_eq!(to_big(a).checked_add(&to_big(b)), Some(to_big(c)));
20622066
}
2063-
fn test_assign(a: Rational, b: isize, c: Rational) {
2067+
fn test_assign(a: Rational64, b: i64, c: Rational64) {
20642068
assert_eq!(a + b, c);
20652069
assert_eq!(
20662070
{
@@ -2117,7 +2121,7 @@ mod test {
21172121

21182122
#[test]
21192123
fn test_sub() {
2120-
fn test(a: Rational, b: Rational, c: Rational) {
2124+
fn test(a: Rational64, b: Rational64, c: Rational64) {
21212125
assert_eq!(a - b, c);
21222126
assert_eq!(
21232127
{
@@ -2131,7 +2135,7 @@ mod test {
21312135
assert_eq!(a.checked_sub(&b), Some(c));
21322136
assert_eq!(to_big(a).checked_sub(&to_big(b)), Some(to_big(c)));
21332137
}
2134-
fn test_assign(a: Rational, b: isize, c: Rational) {
2138+
fn test_assign(a: Rational64, b: i64, c: Rational64) {
21352139
assert_eq!(a - b, c);
21362140
assert_eq!(
21372141
{
@@ -2182,7 +2186,7 @@ mod test {
21822186

21832187
#[test]
21842188
fn test_mul() {
2185-
fn test(a: Rational, b: Rational, c: Rational) {
2189+
fn test(a: Rational64, b: Rational64, c: Rational64) {
21862190
assert_eq!(a * b, c);
21872191
assert_eq!(
21882192
{
@@ -2196,7 +2200,7 @@ mod test {
21962200
assert_eq!(a.checked_mul(&b), Some(c));
21972201
assert_eq!(to_big(a).checked_mul(&to_big(b)), Some(to_big(c)));
21982202
}
2199-
fn test_assign(a: Rational, b: isize, c: Rational) {
2203+
fn test_assign(a: Rational64, b: i64, c: Rational64) {
22002204
assert_eq!(a * b, c);
22012205
assert_eq!(
22022206
{
@@ -2271,7 +2275,7 @@ mod test {
22712275

22722276
#[test]
22732277
fn test_div() {
2274-
fn test(a: Rational, b: Rational, c: Rational) {
2278+
fn test(a: Rational64, b: Rational64, c: Rational64) {
22752279
assert_eq!(a / b, c);
22762280
assert_eq!(
22772281
{
@@ -2285,7 +2289,7 @@ mod test {
22852289
assert_eq!(a.checked_div(&b), Some(c));
22862290
assert_eq!(to_big(a).checked_div(&to_big(b)), Some(to_big(c)));
22872291
}
2288-
fn test_assign(a: Rational, b: isize, c: Rational) {
2292+
fn test_assign(a: Rational64, b: i64, c: Rational64) {
22892293
assert_eq!(a / b, c);
22902294
assert_eq!(
22912295
{
@@ -2360,7 +2364,7 @@ mod test {
23602364

23612365
#[test]
23622366
fn test_rem() {
2363-
fn test(a: Rational, b: Rational, c: Rational) {
2367+
fn test(a: Rational64, b: Rational64, c: Rational64) {
23642368
assert_eq!(a % b, c);
23652369
assert_eq!(
23662370
{
@@ -2372,7 +2376,7 @@ mod test {
23722376
);
23732377
assert_eq!(to_big(a) % to_big(b), to_big(c))
23742378
}
2375-
fn test_assign(a: Rational, b: isize, c: Rational) {
2379+
fn test_assign(a: Rational64, b: i64, c: Rational64) {
23762380
assert_eq!(a % b, c);
23772381
assert_eq!(
23782382
{
@@ -2429,7 +2433,7 @@ mod test {
24292433

24302434
#[test]
24312435
fn test_neg() {
2432-
fn test(a: Rational, b: Rational) {
2436+
fn test(a: Rational64, b: Rational64) {
24332437
assert_eq!(-a, b);
24342438
assert_eq!(-to_big(a), to_big(b))
24352439
}
@@ -2624,7 +2628,7 @@ mod test {
26242628

26252629
#[test]
26262630
fn test_pow() {
2627-
fn test(r: Rational, e: i32, expected: Rational) {
2631+
fn test(r: Rational64, e: i32, expected: Rational64) {
26282632
assert_eq!(r.pow(e), expected);
26292633
assert_eq!(Pow::pow(r, e), expected);
26302634
assert_eq!(Pow::pow(r, &e), expected);
@@ -2635,7 +2639,7 @@ mod test {
26352639
}
26362640

26372641
#[cfg(feature = "num-bigint")]
2638-
fn test_big(r: Rational, e: i32, expected: Rational) {
2642+
fn test_big(r: Rational64, e: i32, expected: Rational64) {
26392643
let r = BigRational::new_raw(r.numer.into(), r.denom.into());
26402644
let expected = BigRational::new_raw(expected.numer.into(), expected.denom.into());
26412645
assert_eq!((&r).pow(e), expected);
@@ -2661,7 +2665,7 @@ mod test {
26612665
#[cfg(feature = "std")]
26622666
fn test_to_from_str() {
26632667
use std::string::{String, ToString};
2664-
fn test(r: Rational, s: String) {
2668+
fn test(r: Rational64, s: String) {
26652669
assert_eq!(FromStr::from_str(&s), Ok(r));
26662670
assert_eq!(r.to_string(), s);
26672671
}
@@ -2675,7 +2679,7 @@ mod test {
26752679
#[test]
26762680
fn test_from_str_fail() {
26772681
fn test(s: &str) {
2678-
let rational: Result<Rational, _> = FromStr::from_str(s);
2682+
let rational: Result<Rational64, _> = FromStr::from_str(s);
26792683
assert!(rational.is_err());
26802684
}
26812685

@@ -2751,7 +2755,7 @@ mod test {
27512755
assert_eq!(_3_2.abs_sub(&_1_2), _1);
27522756
assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero());
27532757
assert_eq!(_1_2.signum(), One::one());
2754-
assert_eq!(_NEG1_2.signum(), -<Ratio<isize>>::one());
2758+
assert_eq!(_NEG1_2.signum(), -<Ratio<i64>>::one());
27552759
assert_eq!(_0.signum(), Zero::zero());
27562760
assert!(_NEG1_2.is_negative());
27572761
assert!(_1_NEG2.is_negative());
@@ -2772,13 +2776,13 @@ mod test {
27722776
assert!(crate::hash(&_0) != crate::hash(&_3_2));
27732777

27742778
// a == b -> hash(a) == hash(b)
2775-
let a = Rational::new_raw(4, 2);
2776-
let b = Rational::new_raw(6, 3);
2779+
let a = Rational64::new_raw(4, 2);
2780+
let b = Rational64::new_raw(6, 3);
27772781
assert_eq!(a, b);
27782782
assert_eq!(crate::hash(&a), crate::hash(&b));
27792783

2780-
let a = Rational::new_raw(123456789, 1000);
2781-
let b = Rational::new_raw(123456789 * 5, 5000);
2784+
let a = Rational64::new_raw(123456789, 1000);
2785+
let b = Rational64::new_raw(123456789 * 5, 5000);
27822786
assert_eq!(a, b);
27832787
assert_eq!(crate::hash(&a), crate::hash(&b));
27842788
}

0 commit comments

Comments
 (0)