#prime #integer #typenum #math #primality #traits #compile-time

no-std typenum-prime

compile-time primality testing of type-level integers from the typenum crate

2 unstable releases

Uses old Rust 2015

0.2.0 Aug 31, 2018
0.1.0 Jan 20, 2018

#310 in No standard library

MIT/Apache

79KB
1.5K SLoC

typenum-prime

This is a rust crate that provides a marker trait for compile-time primality testing of type-level integers from the typenum crate. The test is defined for all unsigned integers.

Usage

The crate is published on https://crates.io/ as typenum-prime. Add it to your Cargo.toml in the usual way.

[dependencies]
typenum-prime = "0.2"

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

I would prefer it if you included language to this effect in the commit message of your first pull request.


lib.rs:

Compile-time primality testing of typenum integers.

The current algorithm is trial division by every prime number from 2 up to next_integer_power_of_two(sqrt(n)). The default compiler recursion limit could sometimes be insufficient, depending on the magnitude of the integer being tested. When necessary, raise it using a crate attribute:

#![recursion_limit="128"]

Example

The intended use of this crate is to put a bound on type-level integer parameters so the compiler enforces their primality. For instance, you might want the number of buckets in a statically-sized hash table to always be a prime number so that hash collisions are reduced. Now you can let the compiler enforce this constraint.

pub struct StaticHashTable<K,V,N>
    where N: Prime + ArrayLength<Option<(K,V)>> {
    buckets: GenericArray<Option<(K,V)>,N>
}

Dependencies

~155KB