diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..de0a554 --- /dev/null +++ b/src/error.rs @@ -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 {} diff --git a/src/generation.rs b/src/generation.rs index f773702..d7a0d51 100644 --- a/src/generation.rs +++ b/src/generation.rs @@ -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. @@ -359,43 +357,6 @@ pub const fn primes_geq( 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; diff --git a/src/lib.rs b/src/lib.rs index ee20306..8e54045 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,7 @@ // 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; @@ -123,7 +124,8 @@ 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};