Skip to content

Commit 3c0e6f1

Browse files
committed
Replace lazy_static with once_cell
1 parent 6f73f0a commit 3c0e6f1

File tree

4 files changed

+93
-102
lines changed

4 files changed

+93
-102
lines changed

Cargo.toml

+2-6
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,8 @@ features = [ "alloc" ]
6262
[dependencies.libm]
6363
version = "0.2.1"
6464

65-
[dependencies.lazy_static]
66-
version = "1.2.0"
67-
default-features = false
68-
# no_std feature is an anti-pattern. Why, lazy_static, why?
69-
# See https://github.com/rust-lang-nursery/lazy-static.rs/issues/150
70-
features = ["spin_no_std"]
65+
[dependencies.once_cell]
66+
version = "1"
7167

7268
[dev-dependencies]
7369
rand_chacha = "0.3"

src/bigrand.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use num_iter::range_step;
1616
use num_traits::Zero;
1717
#[cfg(feature = "prime")]
1818
use num_traits::{FromPrimitive, ToPrimitive};
19+
#[cfg(feature = "prime")]
20+
use once_cell::sync::Lazy;
1921

2022
#[cfg(feature = "prime")]
2123
use crate::prime::probably_prime;
@@ -294,14 +296,12 @@ pub trait RandPrime {
294296
#[cfg(feature = "prime")]
295297
const SMALL_PRIMES: [u8; 15] = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53];
296298

299+
/// The product of the values in SMALL_PRIMES and allows us
300+
/// to reduce a candidate prime by this number and then determine whether it's
301+
/// coprime to all the elements of SMALL_PRIMES without further BigUint
302+
/// operations.
297303
#[cfg(feature = "prime")]
298-
lazy_static! {
299-
/// The product of the values in SMALL_PRIMES and allows us
300-
/// to reduce a candidate prime by this number and then determine whether it's
301-
/// coprime to all the elements of SMALL_PRIMES without further BigUint
302-
/// operations.
303-
static ref SMALL_PRIMES_PRODUCT: BigUint = BigUint::from_u64(16_294_579_238_595_022_365).unwrap();
304-
}
304+
const SMALL_PRIMES_PRODUCT: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(16_294_579_238_595_022_365).unwrap());
305305

306306
#[cfg(feature = "prime")]
307307
impl<R: Rng + ?Sized> RandPrime for R {

src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ extern crate std;
111111
extern crate smallvec;
112112

113113
#[cfg(feature = "prime")]
114-
#[macro_use]
115-
extern crate lazy_static;
114+
extern crate once_cell;
116115

117116
extern crate num_integer as integer;
118117

src/prime.rs

+83-87
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
use integer::Integer;
55
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
6+
use once_cell::sync::Lazy;
67
use rand::rngs::StdRng;
78
use rand::SeedableRng;
89

@@ -12,12 +13,9 @@ use crate::bigrand::RandBigInt;
1213
use crate::Sign::Plus;
1314
use crate::{BigInt, BigUint, IntoBigUint};
1415

15-
lazy_static! {
16-
pub(crate) static ref BIG_1: BigUint = BigUint::one();
17-
pub(crate) static ref BIG_2: BigUint = BigUint::from_u64(2).unwrap();
18-
pub(crate) static ref BIG_3: BigUint = BigUint::from_u64(3).unwrap();
19-
pub(crate) static ref BIG_64: BigUint = BigUint::from_u64(64).unwrap();
20-
}
16+
pub(crate) const BIG_1: Lazy<BigUint> = Lazy::new(|| BigUint::one());
17+
pub(crate) const BIG_2: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(2).unwrap());
18+
pub(crate) const BIG_64: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(64).unwrap());
2119

2220
const PRIMES_A: u64 = 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 37;
2321
const PRIMES_B: u64 = 29 * 31 * 41 * 43 * 47 * 53;
@@ -439,8 +437,7 @@ mod tests {
439437

440438
use crate::biguint::ToBigUint;
441439

442-
lazy_static! {
443-
static ref PRIMES: Vec<&'static str> = vec![
440+
const PRIMES: &'static [&'static str] = &[
444441
"2",
445442
"3",
446443
"5",
@@ -469,85 +466,84 @@ mod tests {
469466
"9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576599", // E-382: 2^382-105
470467
"42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472367", // Curve41417: 2^414-17
471468
"6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151", // E-521: 2^521-1
472-
];
473-
474-
static ref COMPOSITES: Vec<&'static str> = vec![
475-
"0",
476-
"1",
477-
478-
"21284175091214687912771199898307297748211672914763848041968395774954376176754",
479-
"6084766654921918907427900243509372380954290099172559290432744450051395395951",
480-
"84594350493221918389213352992032324280367711247940675652888030554255915464401",
481-
"82793403787388584738507275144194252681",
482-
483-
// Arnault, "Rabin-Miller Primality Test: Composite Numbers Which Pass It",
484-
// Mathematics of Computation, 64(209) (January 1995), pp. 335-361.
485-
"1195068768795265792518361315725116351898245581", // strong pseudoprime to prime bases 2 through 29
486-
// strong pseudoprime to all prime bases up to 200
487-
"8038374574536394912570796143419421081388376882875581458374889175222974273765333652186502336163960045457915042023603208766569966760987284043965408232928738791850869166857328267761771029389697739470167082304286871099974399765441448453411558724506334092790222752962294149842306881685404326457534018329786111298960644845216191652872597534901",
488-
489-
// Extra-strong Lucas pseudoprimes. https://oeis.org/A217719
490-
"989",
491-
"3239",
492-
"5777",
493-
"10877",
494-
"27971",
495-
"29681",
496-
"30739",
497-
"31631",
498-
"39059",
499-
"72389",
500-
"73919",
501-
"75077",
502-
"100127",
503-
"113573",
504-
"125249",
505-
"137549",
506-
"137801",
507-
"153931",
508-
"155819",
509-
"161027",
510-
"162133",
511-
"189419",
512-
"218321",
513-
"231703",
514-
"249331",
515-
"370229",
516-
"429479",
517-
"430127",
518-
"459191",
519-
"473891",
520-
"480689",
521-
"600059",
522-
"621781",
523-
"632249",
524-
"635627",
525-
526-
"3673744903",
527-
"3281593591",
528-
"2385076987",
529-
"2738053141",
530-
"2009621503",
531-
"1502682721",
532-
"255866131",
533-
"117987841",
534-
"587861",
535-
536-
"6368689",
537-
"8725753",
538-
"80579735209",
539-
"105919633",
540-
];
541-
542-
// Test Cases from #51
543-
static ref ISSUE_51: Vec<&'static str> = vec![
544-
"1579751",
545-
"1884791",
546-
"3818929",
547-
"4080359",
548-
"4145951",
549-
];
550-
}
469+
];
470+
471+
const COMPOSITES: &'static [&'static str] = &[
472+
"0",
473+
"1",
474+
475+
"21284175091214687912771199898307297748211672914763848041968395774954376176754",
476+
"6084766654921918907427900243509372380954290099172559290432744450051395395951",
477+
"84594350493221918389213352992032324280367711247940675652888030554255915464401",
478+
"82793403787388584738507275144194252681",
479+
480+
// Arnault, "Rabin-Miller Primality Test: Composite Numbers Which Pass It",
481+
// Mathematics of Computation, 64(209) (January 1995), pp. 335-361.
482+
"1195068768795265792518361315725116351898245581", // strong pseudoprime to prime bases 2 through 29
483+
// strong pseudoprime to all prime bases up to 200
484+
"8038374574536394912570796143419421081388376882875581458374889175222974273765333652186502336163960045457915042023603208766569966760987284043965408232928738791850869166857328267761771029389697739470167082304286871099974399765441448453411558724506334092790222752962294149842306881685404326457534018329786111298960644845216191652872597534901",
485+
486+
// Extra-strong Lucas pseudoprimes. https://oeis.org/A217719
487+
"989",
488+
"3239",
489+
"5777",
490+
"10877",
491+
"27971",
492+
"29681",
493+
"30739",
494+
"31631",
495+
"39059",
496+
"72389",
497+
"73919",
498+
"75077",
499+
"100127",
500+
"113573",
501+
"125249",
502+
"137549",
503+
"137801",
504+
"153931",
505+
"155819",
506+
"161027",
507+
"162133",
508+
"189419",
509+
"218321",
510+
"231703",
511+
"249331",
512+
"370229",
513+
"429479",
514+
"430127",
515+
"459191",
516+
"473891",
517+
"480689",
518+
"600059",
519+
"621781",
520+
"632249",
521+
"635627",
522+
523+
"3673744903",
524+
"3281593591",
525+
"2385076987",
526+
"2738053141",
527+
"2009621503",
528+
"1502682721",
529+
"255866131",
530+
"117987841",
531+
"587861",
532+
533+
"6368689",
534+
"8725753",
535+
"80579735209",
536+
"105919633",
537+
];
538+
539+
// Test Cases from #51
540+
const ISSUE_51: &'static [&'static str] = &[
541+
"1579751",
542+
"1884791",
543+
"3818929",
544+
"4080359",
545+
"4145951",
546+
];
551547

552548
#[test]
553549
fn test_primes() {

0 commit comments

Comments
 (0)