diff --git a/src/lib.rs b/src/lib.rs index 8e6daf9..5032028 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,14 +82,14 @@ A prime generator, using the Trial Division method. Create with `let mut pset = TrialDivision::new()`, and then use `pset.iter()` to iterate over all primes. **/ -#[derive(Default, Clone)] +#[derive(Clone)] pub struct TrialDivision { lst: Vec, } const WHEEL30: [u64; 8] = [1, 7, 11, 13, 17, 19, 23, 29]; -#[derive(Default, Copy, Clone)] +#[derive(Copy, Clone)] struct Wheel30 { base: u64, ix: usize, @@ -107,13 +107,19 @@ impl Wheel30 { } } +impl Default for Wheel30 { + fn default() -> Self { + Wheel30 { base: 0, ix: 1 } + } +} + /** A prime generator, using the Sieve of Eratosthenes method. This is asymptotically more efficient than the Trial Division method, but slower earlier on. Create with `let mut pset = Sieve::new()`, and then use `pset.iter()` to iterate over all primes. **/ -#[derive(Default, Clone)] +#[derive(Clone)] pub struct Sieve { primes: Vec, wheel: Wheel30, @@ -142,6 +148,12 @@ impl TrialDivision { } } +impl Default for TrialDivision { + fn default() -> Self { + Self::new() + } +} + impl PrimeSetBasics for TrialDivision { /// Finds one more prime, and adds it to the list fn expand(&mut self) { @@ -170,6 +182,12 @@ impl PrimeSetBasics for TrialDivision { } } +impl Default for Sieve { + fn default() -> Self { + Self::new() + } +} + impl Sieve { /// A new prime generator, primed with 2 and 3 pub fn new() -> Sieve { diff --git a/tests/basictests.rs b/tests/basictests.rs index 70939ff..7e4dfab 100644 --- a/tests/basictests.rs +++ b/tests/basictests.rs @@ -145,3 +145,13 @@ fn test_sieve() { assert_eq!(sieved, trialled); } + +// Test that the Sieve Default method works +#[test] +fn test_default() { + let mut sieve = Sieve::default(); + + let n = sieve.get(5); + + assert_eq!(n, 13); +}