Skip to content

Commit

Permalink
Move Error into its own module file (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSorngard authored May 7, 2024
1 parent 75460e1 commit 4908f03
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
40 changes: 40 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Contains the implementation of the error type that is returned by the segmented sieving and generation functions.
use core::fmt;

/// The error returned by [`primes_lt`](crate::primes_lt) and [`primes_geq`](crate::primes_geq) if the input
/// is invalid or does not work to produce the requested primes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Error {
/// The limit was larger than `MEM^2`.
TooLargeLimit(u64, u64),
/// The limit was smaller than or equal to 2.
TooSmallLimit(u64),
/// Encountered a number larger than `MEM`^2.
SieveOverrun(u64),
/// Ran out of primes.
OutOfPrimes,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::TooLargeLimit(limit, mem_sqr) => write!(
f,
"the limit ({limit}) was larger than `MEM`^2 ({mem_sqr})"
),
Self::TooSmallLimit(limit) => write!(
f,
"the limit was {limit}, which is smaller than or equal to 2"
),
Self::SieveOverrun(number) => write!(
f,
"encountered the number {number} which would have needed `MEM` to be at least {} to sieve", crate::imath::isqrt(*number) + 1
),
Self::OutOfPrimes => write!(f, "ran out of primes before the array was filled"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
41 changes: 1 addition & 40 deletions src/generation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use core::fmt;

use crate::{sieve, sieving::sieve_segment, Underlying};
use crate::{error::Error, sieve, sieving::sieve_segment, Underlying};

/// Returns the `N` first prime numbers.
/// Fails to compile if `N` is 0.
Expand Down Expand Up @@ -359,43 +357,6 @@ pub const fn primes_geq<const N: usize, const MEM: usize>(
Ok(primes)
}

/// The error returned by [`primes_lt`] and [`primes_geq`] if the input
/// is invalid or does not work to produce the requested primes.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Error {
/// The limit was larger than `MEM^2`.
TooLargeLimit(u64, u64),
/// The limit was smaller than or equal to 2.
TooSmallLimit(u64),
/// Encountered a number larger than `MEM`^2.
SieveOverrun(u64),
/// Ran out of primes.
OutOfPrimes,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::TooLargeLimit(limit, mem_sqr) => write!(
f,
"the limit ({limit}) was larger than `MEM`^2 ({mem_sqr})"
),
Self::TooSmallLimit(limit) => write!(
f,
"the limit was {limit}, which is smaller than or equal to 2"
),
Self::SieveOverrun(number) => write!(
f,
"encountered the number {number} which would have needed `MEM` to be at least {} to sieve", crate::imath::isqrt(*number) + 1
),
Self::OutOfPrimes => write!(f, "ran out of primes before the array was filled"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

#[cfg(test)]
mod test {
use crate::is_prime;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@
// This is used since there is currently no way to be generic over types that can do arithmetic at compile time.
type Underlying = u32;

mod error;
mod generation;
mod imath;
mod miller_rabin;
mod other_prime;
mod sieving;
mod wrapper;

pub use generation::{primes, primes_geq, primes_lt, Error};
pub use error::Error;
pub use generation::{primes, primes_geq, primes_lt};
pub use imath::isqrt;
pub use miller_rabin::is_prime;
pub use other_prime::{next_prime, previous_prime};
Expand Down

0 comments on commit 4908f03

Please sign in to comment.