ruvector-collections 2.2.3

High-performance collection management for Ruvector vector databases
Documentation
// build.rs — emits PRIMES_BELOW_2K[57] and PRIMES_ABOVE_2K[57] using the
// same Miller-Rabin kernel that ships at runtime. ADR-151 acceptance #2
// requires the table and the runtime to agree on every entry, and this is
// how we guarantee that — one source of truth, included from both sides.

use std::env;
use std::fs;
use std::path::PathBuf;

include!("src/primality_kernel.rs");

fn main() {
    println!("cargo:rerun-if-changed=src/primality_kernel.rs");
    println!("cargo:rerun-if-changed=build.rs");

    let mut out = String::with_capacity(4096);
    out.push_str(
        "// AUTO-GENERATED by build.rs from primality_kernel.rs.\n\
         // Do not edit by hand — regenerated on every build.\n\
         //\n\
         // Index: table[k - 8] holds the prime for exponent k, k in [8, 64].\n\n",
    );

    // BELOW: largest prime strictly less than 2^k.
    out.push_str(
        "/// Largest prime strictly less than 2^k for k in [8, 64], indexed by `k - 8`.\n\
         ///\n\
         /// Generated at build time from the same Miller-Rabin kernel that ships at runtime\n\
         /// (ADR-151 acceptance #2). Re-validated under `cargo test`.\n",
    );
    out.push_str("pub const PRIMES_BELOW_2K: [u64; 57] = [\n");
    for k in 8u32..=64 {
        let p = if k == 64 {
            // 2^64 overflows u64. Largest prime < 2^64 is the largest u64
            // prime; u64::MAX itself is composite, so prev_prime(u64::MAX)
            // gives the right answer.
            mr_prev_prime_u64(u64::MAX)
        } else {
            mr_prev_prime_u64(1u64 << k)
        };
        out.push_str(&format!("    {p}, // largest prime < 2^{k}\n"));
    }
    out.push_str("];\n\n");

    // ABOVE: smallest prime strictly greater than 2^k.
    out.push_str(
        "/// Smallest prime strictly greater than 2^k for k in [8, 64], indexed by `k - 8`.\n\
         ///\n\
         /// Entry at k = 64 is `0` (sentinel) — no u64 prime exists greater than 2^64.\n\
         /// Runtime callers must avoid that index.\n",
    );
    out.push_str("pub const PRIMES_ABOVE_2K: [u64; 57] = [\n");
    for k in 8u32..=64 {
        let p = if k == 64 {
            // No u64 prime exists strictly greater than 2^64. Emit a sentinel
            // and forbid this index at the runtime call site (debug_assert
            // in next_prime_above_pow2).
            0u64
        } else {
            mr_next_prime_u64(1u64 << k)
        };
        if p == 0 {
            out.push_str(&format!("    0, // sentinel: no u64 prime > 2^{k}\n"));
        } else {
            out.push_str(&format!("    {p}, // smallest prime > 2^{k}\n"));
        }
    }
    out.push_str("];\n");

    let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR not set"));
    let out_path = out_dir.join("prime_tables.rs");
    fs::write(&out_path, out).expect("failed to write prime_tables.rs");
}