Skip to content

Commit 87e6e3d

Browse files
committed
Updated for more modern Rust
1 parent 8d3d3a1 commit 87e6e3d

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "primes"
4-
version = "0.2.0"
4+
version = "0.2.1"
55
authors = ["Wendell Smith <wendellwsmith@gmail.com>"]
66
license="BSD-3-Clause"
77

benches/benches.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#![feature(test)]
22

3-
extern crate test;
43
extern crate primes;
4+
extern crate test;
55
use primes::PrimeSet;
66

77
use test::Bencher;
88

99
#[bench]
10-
fn bench_primes(b : &mut Bencher){
10+
fn bench_primes(b: &mut Bencher) {
1111
b.iter(|| {
1212
let mut pset = PrimeSet::new();
13-
let (_, _) = pset.find(1_000_000);
14-
//~ let (idx, n) = pset.find(1_000_000);
15-
//~ println!("Prime {}: {}", idx, n);
16-
})
13+
let (_, _) = pset.find(1_000_000);
14+
//~ let (idx, n) = pset.find(1_000_000);
15+
//~ println!("Prime {}: {}", idx, n);
16+
})
1717
}
18-

src/lib.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,14 @@ case, but slower in the long term as they do not use any caching of primes.
6363
#![doc(html_root_url = "https://wackywendell.github.io/primes/")]
6464

6565
use std::cmp::Ordering::{Equal, Greater, Less};
66-
#[warn(non_camel_case_types)]
67-
#[warn(non_snake_case)]
68-
#[warn(unused_qualifications)]
69-
#[warn(non_upper_case_globals)]
70-
#[warn(missing_docs)]
7166
use std::ops::Index;
7267
use std::slice;
7368

7469
/** A prime generator, using the Sieve of Eratosthenes.
7570
7671
Create with `let mut pset = PrimeSet::new()`, and then use `pset.iter()` to iterate over all primes.
7772
**/
73+
#[derive(Default)]
7874
pub struct PrimeSet {
7975
lst: Vec<u64>,
8076
}
@@ -98,7 +94,7 @@ impl PrimeSet {
9894
let mut l: u64 = self.lst[self.lst.len() - 1] + 2;
9995
let mut remainder = 0;
10096
loop {
101-
for &n in self.lst.iter() {
97+
for &n in &self.lst {
10298
remainder = l % n;
10399
if remainder == 0 || n * n > l {
104100
break;
@@ -119,13 +115,17 @@ impl PrimeSet {
119115
self.lst.len()
120116
}
121117

118+
pub fn is_empty(&self) -> bool {
119+
false
120+
}
121+
122122
/// Return all primes found so far as a slice
123-
pub fn list<'a>(&'a self) -> &'a [u64] {
123+
pub fn list(&self) -> &[u64] {
124124
&self.lst[..]
125125
}
126126

127127
/// Iterator over all primes not yet found
128-
pub fn generator<'a>(&'a mut self) -> PrimeSetIter<'a> {
128+
pub fn generator(&mut self) -> PrimeSetIter {
129129
let myn = self.len();
130130
PrimeSetIter {
131131
p: self,
@@ -136,7 +136,7 @@ impl PrimeSet {
136136

137137
/// Iterator over all primes, starting with 2. If you don't care about the "state" of the
138138
/// PrimeSet, this is what you want!
139-
pub fn iter<'a>(&'a mut self) -> PrimeSetIter<'a> {
139+
pub fn iter(&mut self) -> PrimeSetIter {
140140
PrimeSetIter {
141141
p: self,
142142
n: 0,
@@ -149,7 +149,7 @@ impl PrimeSet {
149149
//~ }
150150

151151
/// Iterator over just the primes found so far
152-
pub fn iter_vec<'a>(&'a self) -> slice::Iter<'a, u64> {
152+
pub fn iter_vec(&self) -> slice::Iter<u64> {
153153
self.lst.iter()
154154
}
155155

@@ -166,6 +166,7 @@ impl PrimeSet {
166166
/// Check if a number is prime
167167
/// Note that this only requires primes up to n.sqrt() to be generated, and will generate
168168
/// them as necessary on its own.
169+
#[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
169170
pub fn is_prime(&mut self, n: u64) -> bool {
170171
if n <= 1 {
171172
return false;
@@ -208,7 +209,7 @@ impl PrimeSet {
208209
}
209210
lim >>= 1;
210211
}
211-
return Some((base, self.lst[base]));
212+
Some((base, self.lst[base]))
212213
}
213214

214215
/// Get the nth prime, even if we haven't yet found it
@@ -257,9 +258,10 @@ impl<'a> Iterator for PrimeSetIter<'a> {
257258
type Item = u64;
258259
fn next(&mut self) -> Option<u64> {
259260
while self.n >= self.p.len() {
260-
match self.expand {
261-
true => self.p.expand(),
262-
false => return None,
261+
if self.expand {
262+
self.p.expand()
263+
} else {
264+
return None;
263265
}
264266
}
265267
self.n += 1;
@@ -282,7 +284,8 @@ fn firstfac(x: u64) -> u64 {
282284
return n;
283285
};
284286
}
285-
return x;
287+
// No factor found. It must be prime.
288+
x
286289
}
287290

288291
/// Find all prime factors of a number
@@ -302,7 +305,7 @@ pub fn factors(x: u64) -> Vec<u64> {
302305
curn /= m
303306
};
304307
}
305-
return lst;
308+
lst
306309
}
307310

308311
/// Find all unique prime factors of a number
@@ -325,7 +328,7 @@ pub fn factors_uniq(x: u64) -> Vec<u64> {
325328
break;
326329
}
327330
}
328-
return lst;
331+
lst
329332
}
330333

331334
/// Test whether a number is prime. Checks every odd number up to sqrt(n).

0 commit comments

Comments
 (0)