Skip to content

Commit

Permalink
Removes the std and const_assert features. Increased MSRV to 1.81.0.
Browse files Browse the repository at this point in the history
The `const_assert` feature is now "always on" and the crate now uses `Error` in `core`, and is always `no_std` compatible.
  • Loading branch information
JSorngard committed Sep 5, 2024
1 parent 2ebc7a1 commit 798861c
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 172 deletions.
5 changes: 5 additions & 0 deletions .markdownlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"no-duplicate-heading": {
"siblings_only": true
}
}
146 changes: 88 additions & 58 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,109 +1,139 @@
# Changelog

This file contains the changes to the crate since version 0.4.8.

# 0.8.7
## 0.9.0

### Breaking changes

- Removed the `const_assert` feature, it is now always enabled.
- Removed the `std` feature, the crate now uses the `Error` trait
from `core`. The crate is thus always `no_std` compatible.

These changes mean that the MSRV of the crate is increased from 1.67.1 to 1.81.0.

## 0.8.7

- Sped up `is_prime` by checking fewer witnesses in the Miller-Rabin test.
- Sped up `is_prime` by checking fewer witnesses in the Miller-Rabin test.

# 0.8.6
## 0.8.6

- Fixed a bug where the crate would try to sieve numbers below zero for some inputs to `sieve_lt` and `primes_lt` and panic.
- Fixed a bug where the crate would try to sieve numbers below zero for some
inputs to `sieve_lt` and `primes_lt` and panic.

# 0.8.5
## 0.8.5

- Added the `const_assert` feature that promotes all panics that involve only const generics into compile errors.
- Added the `const_assert` feature that promotes all panics that involve only
const generics into compile errors.

# 0.8.4
## 0.8.4

- License and docs.rs link improvements in README.
- Minor documentation improvements.
- License and docs.rs link improvements in README.
- Minor documentation improvements.

# 0.8.3
## 0.8.3

- Added the `totient` function to the `Primes` struct.
- Derived `Serialize` and `Deserialize` for the error types.
- Derived `Hash` for `SieveError`.
- Added the `totient` function to the `Primes` struct.
- Derived `Serialize` and `Deserialize` for the error types.
- Derived `Hash` for `SieveError`.

# 0.8.2
## 0.8.2

- Added the `serde` feature that derives the `Serialize` and `Deserialize` traits from `serde` for the `Primes` struct.
- Added the `serde` feature that derives the `Serialize` and `Deserialize`
traits from `serde` for the `Primes` struct.

# 0.8.1
## 0.8.1

- Added a crate feature flag badge to the docs.
- Mention what can be done with the crate clearer in the description.
- Added a crate feature flag badge to the docs.
- Mention what can be done with the crate clearer in the description.

# 0.8.0
## 0.8.0

## Breaking changes
### Breaking changes

- Changed `Primes<N>::binary_search` to have the same API as `slice::binary_search`.
- Changed `Primes<N>::binary_search` to have the same API as `slice::binary_search`.

# 0.7.4
## 0.7.4

- Corrected wrong doclink to `remainder` in docstring of `Primes<N>::prime_factors`.
- Corrected wrong doclink to `remainder` in docstring of `Primes<N>::prime_factors`.

# 0.7.3
## 0.7.3

- Added `Primes<N>::prime_factors` function that returns an iterator over the prime factors of the given number (and not their multiplicities).
- Added `Primes<N>::prime_factors` function that returns an iterator over the
prime factors of the given number (and not their multiplicities).

# 0.7.2
## 0.7.2

- Added `Primes<N>::prime_factorization` function that returns an iterator over the prime factors of the given number and their multiplicities.
- Added `Primes<N>::prime_factorization` function that returns an iterator over
the prime factors of the given number and their multiplicities.

# 0.7.1
## 0.7.1

- Organized the examples in the readme and top level crate documentation in a clearer way.
- Organized the examples in the readme and top level crate documentation in a
clearer way.

# 0.7.0
## 0.7.0

## Breaking changes
### Breaking changes

- `PrimesIter` no longer takes a const generic.
- `PrimesIter` no longer takes a const generic.

# 0.6.2
## 0.6.2

- Minor documentation tweaks.
- Minor documentation tweaks.

# 0.6.1
## 0.6.1

- Corrected MSRV to 1.67.1.
- Corrected MSRV to 1.67.1.

# 0.6.0
## 0.6.0

## Breaking changes
### Breaking changes

- Made the `Primes<N>::iter` function and the `IntoIterator` implementation for `Primes<N>` return custom iterator types. Allows less disruptive refactoring in the future.
- Made the `Primes<N>::iter` function and the `IntoIterator` implementation for
`Primes<N>` return custom iterator types. Allows less disruptive refactoring in
the future.

## Other changes
### Other changes

- Removed panics in functions (like `primes`) when `N` is zero. It results in an empty array, but may be what you want.
- Removed panics in functions (like `primes`) when `N` is zero. It results in an
empty array, but may be what you want.

# 0.5.1
## 0.5.1

- Implemented `IntoIterator` for `&Primes<N>`.
- Implemented `IntoIterator` for `&Primes<N>`.

# 0.5.0
## 0.5.0

This version focuses on adding support for generating primes and sieving numbers in arbitrary ranges, instead of always having to start from 0.
This version focuses on adding support for generating primes and sieving numbers
in arbitrary ranges, instead of always having to start from 0.
It also shortens and clarifies some function names.

## Breaking changes
### Breaking changes

- Renamed `are_prime` to `sieve`.
- Renamed `are_prime_below` to `sieve_lt`.
- Changed function signature of `sieve_lt`.
- Renamed `largest_prime_leq` to `previous_prime`.
- Renamed `smallest_prime_lt` to `next_prime`.
- Renamed `prime_counts` to `count_primes`.
- Removed `moebius`, as it is out of scope of this crate. If you want the source code for that function it can be found on [Rosettacode](https://rosettacode.org/wiki/M%C3%B6bius_function#Rust), or in older versions of this crate.
- Renamed `are_prime` to `sieve`.
- Renamed `are_prime_below` to `sieve_lt`.
- Changed function signature of `sieve_lt`.
- Renamed `largest_prime_leq` to `previous_prime`.
- Renamed `smallest_prime_lt` to `next_prime`.
- Renamed `prime_counts` to `count_primes`.
- Removed `moebius`, as it is out of scope of this crate. If you want the source
code for that function it can be found on [Rosettacode](https://rosettacode.org/wiki/M%C3%B6bius_function#Rust),
or in older versions of this crate.

## New features

- Added `primes_geq`, `primes_lt`, and `sieve_geq` functions to work with arbitrary ranges. They take two const generics, the number of values to return and the size of the sieve used during evaluation.
- Added `primes_segment!` and `sieve_segment!` macros to simplify usage of the above functions. These macros compute the size of the sieve that the above functions need. Due to restrictions on const arithmetic this can not be done inside the functions.
- Added `isqrt` function. This can be useful if you wish to compute the size of the sieve yourself.
- Added `primes_geq`, `primes_lt`, and `sieve_geq` functions to work with
arbitrary ranges. They take two const generics, the number of values to return
and the size of the sieve used during evaluation.
- Added `primes_segment!` and `sieve_segment!` macros to simplify usage of the
above functions. These macros compute the size of the sieve that the above
functions need. Due to restrictions on const arithmetic this can not be done
inside the functions.
- Added `isqrt` function. This can be useful if you wish to compute the size of
the sieve yourself.

## Minor changes

- Speed up `PRIMES::count_primes_leq` by using a binary instead of linear search.
- Various documentation improvements.
- Speed up `PRIMES::count_primes_leq` by using a binary instead of linear search.
- Various documentation improvements.
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ keywords = ["const", "primes", "no_std", "prime-numbers"]
categories = ["mathematics", "no-std", "no-std::no-alloc", "algorithms"]
description = "Work with prime numbers in const contexts. Prime generation, primality testing, prime counting, and more."
repository = "https://github.com/JSorngard/const-primes/"
rust-version = "1.67.1"

[dependencies]
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
Expand All @@ -19,10 +18,6 @@ criterion = { version = "0.5", features = ["html_reports"] }
rand = "0.8"

[features]
# Implements the `Error` trait from the standard library for the error types.
std = []
# Promotes panics that involve only const generics into compile errors. Increases the MSRV of the crate to 1.79.0.
const_assert = []
# Derives the `Serialize` and `Deserialize` traits from [`serde`](https://crates.io/crates/serde) for the `Primes` struct, as well as a few others.
serde = ["dep:serde", "dep:serde_arrays"]

Expand Down
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# const-primes
# const-primes

[![Static Badge](https://img.shields.io/badge/github-JSorngard%2Fconst--primes-8da0cb?logo=github)](https://github.com/JSorngard/const-primes)
[![Crates.io Version](https://img.shields.io/crates/v/const_primes?logo=rust)](https://crates.io/crates/const-primes)
Expand All @@ -8,16 +8,18 @@

Generate and work with prime numbers in const contexts.

This crate lets you for example pre-compute prime numbers at compile time, store them in the binary, and use them later for related computations,
This crate lets you for example pre-compute prime numbers at compile time, store
them in the binary, and use them later for related computations,
or check whether a number is prime in a const function.

`#![no_std]` compatible, and currently supports Rust versions 1.67.1 or newer, though enabling feature flags may increase this.
`#![no_std]` compatible.

## Example: generate primes at compile time and use them for related computations

The struct `Primes` is a wrapper around an array of primes generated by a
The struct `Primes` is a wrapper around an array of primes generated by a
[segmented sieve of Eratosthenes](https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Segmented_sieve)
and can be used as a cache of prime numbers for related computations:

```rust
// The first 100 primes
const CACHE: Primes<100> = Primes::new();
Expand All @@ -41,7 +43,9 @@ assert_eq!(CACHE.prime_factorization(3072).collect(), &[(2, 10), (3, 1)])
assert!(CACHE.is_prime(1000).is_none());
assert!(CACHE.count_primes_leq(1000).is_none());
```

Want only the numbers? Use the `primes` function, or convert the cache into an array:

```rust
use const_primes::{primes, Primes};

Expand All @@ -57,6 +61,7 @@ assert_eq!(PRIMES_ARRAY1, PRIMES_ARRAY2);
## Example: primality checking

Use `is_prime` to test whether a given number is prime:

```rust
use const_primes::is_prime;

Expand All @@ -67,10 +72,16 @@ assert!(CHECK);

## Example: generate the three primes after 5000000031

The crate also provides prime generation and sieving functions that can be used to work with ranges that don't start at zero, e.g. `primes_geq` and `sieve_lt`. These functions can use large sieves to compute large primes, but don't need to return the entire sieve, just the requested numbers.
They are most conveniently used through the macros `primes_segment!` and `sieve_segment!` that automatically compute the size of the sieve that's needed for a certain computation.
The crate also provides prime generation and sieving functions that can be used
to work with ranges that don't start at zero, e.g. `primes_geq` and `sieve_lt`.
These functions can use large sieves to compute large primes, but don't need to
return the entire sieve, just the requested numbers.
They are most conveniently used through the macros `primes_segment!` and
`sieve_segment!` that automatically compute the size of the sieve that's needed
for a certain computation.

Compute 3 primes greater than or equal to 5000000031:

```rust
use const_primes::{primes_segment, GenerationError};

Expand All @@ -82,7 +93,9 @@ assert_eq!(PRIMES_GEQ, Ok([5_000_000_039, 5_000_000_059, 5_000_000_063]));

## Example: find the next or previous prime numbers

Find the next or previous prime numbers with `next_prime` and `previous_prime` if they exist and can be represented in a `u64`:
Find the next or previous prime numbers with `next_prime` and `previous_prime`
if they exist and can be represented in a `u64`:

```rust
use const_primes::{previous_prime, next_prime};

Expand All @@ -96,22 +109,24 @@ assert_eq!(PREV, Some(23));
assert_eq!(NO_SUCH, None);
assert_eq!(TOO_BIG, None);
```

and more!

## Feature flags

`std`: implements the `Error` trait from the standard library for the error types.

`serde`: derives the `Serialize` and `Deserialize` traits from [`serde`](https://crates.io/crates/serde) for the `Primes` struct, as well as a few others.

`const_assert`: promotes panics that involve only const generics into compile errors. Increases the MSRV of the crate to 1.79.0.
`serde`: derives the `Serialize` and `Deserialize` traits from [`serde`](https://crates.io/crates/serde)
for the `Primes` struct, as well as a few others.

## License

Licensed under either of

* Apache License, Version 2.0 ([LICENSE-APACHE](https://github.com/JSorngard/const-primes/blob/main/LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
* MIT license ([LICENSE-MIT](https://github.com/JSorngard/const-primes/blob/main/LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
- Apache License, Version 2.0 ([LICENSE-APACHE](https://github.com/JSorngard/const-primes/blob/main/LICENSE-APACHE)
or <http://www.apache.org/licenses/LICENSE-2.0>).
- MIT license ([LICENSE-MIT](https://github.com/JSorngard/const-primes/blob/main/LICENSE-MIT)
or <http://opensource.org/licenses/MIT>).

at your option.

Expand Down
18 changes: 2 additions & 16 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<const N: usize> Primes<N> {
/// If any of the primes overflow a `u32` it will panic in const contexts or debug mode.
#[must_use = "the associated method only returns a new value"]
pub const fn new() -> Self {
feature_gated_inline_const!(assert!(N > 0, "`N` must be at least 1"));
const { assert!(N > 0, "`N` must be at least 1") }
Self(primes())
}

Expand Down Expand Up @@ -546,7 +546,7 @@ impl<const N: usize> Default for Primes<N> {
/// Panics if `N` is 0.
/// This is always a compile error instead of a panic if the `const_assert` feature is enabled.
fn default() -> Self {
feature_gated_inline_const!(assert!(N > 0, "`N` must be at least 1"));
const { assert!(N > 0, "`N` must be at least 1") }
Self(primes())
}
}
Expand Down Expand Up @@ -955,18 +955,4 @@ mod test {
}
}
}

#[cfg(not(feature = "const_assert"))]
#[test]
#[should_panic]
fn check_panic_on_empty_new() {
let _primes = Primes::<0>::new();
}

#[cfg(not(feature = "const_assert"))]
#[test]
#[should_panic]
fn test_panic_on_empty_default() {
let _primes = Primes::<0>::default();
}
}
Loading

0 comments on commit 798861c

Please sign in to comment.