num-valid 0.3.3

A robust numerical library providing validated types for real and complex numbers to prevent common floating-point errors like NaN propagation. Features a generic, layered architecture with support for native f64 and optional arbitrary-precision arithmetic.
Documentation
#![deny(rustdoc::broken_intra_doc_links)]

//! # Numerical Kernels and Validated Types
//!
//! This module is the core of the numerical backend for the [`num-valid`](crate) crate.
//! It defines the fundamental traits, types, and operations for floating-point arithmetic.
//!
//! ## Core Architecture
//!
//! The architecture is built on three key concepts:
//!
//! 1.  **Raw Traits ([`RawScalarTrait`], [`RawRealTrait`], [`RawComplexTrait`]):**
//!     These traits define the baseline contract for a "raw" number type (like [`f64`] or
//!     [`rug::Float`](https://docs.rs/rug/latest/rug/struct.Float.html)).
//!     They provide a comprehensive set of `unchecked_*` methods for arithmetic and mathematical
//!     functions. The "unchecked" prefix signifies that these methods do **not** perform
//!     any validation (e.g., for domain, finiteness, or division by zero) and are intended
//!     for internal use where validity has already been confirmed.
//!
//! 2.  **Validated Structs ([`RealValidated`], [`ComplexValidated`]):**
//!     These are wrapper types (newtypes) that enforce correctness. An instance of `RealValidated<P>`
//!     is guaranteed to hold a raw value that has been successfully validated against the policy `P`.
//!     All operations on these types (addition, `sqrt`, etc.) are designed to preserve this validity,
//!     either by returning a `Result` or by panicking on failure in debug builds.
//!
//! 3.  **Validation Policies ([`NumKernel`]):**
//!     This trait allows the validation strategy to be generic. A kernel can be configured with
//!     different policies, such as [`NumKernelStrictFinite`], which ensures all numbers are
//!     finite (not NaN or Infinity).
//!
//! ## Available Kernels
//!
//! The library provides concrete kernels that bundle these concepts:
//!
//! - **`native64`:** Uses Rust's standard [`f64`] and `num::Complex<f64>` as the raw types.
//!   This is the default, high-performance kernel.
//!
//! - **`rug` (feature-gated):** Uses arbitrary-precision types from the [`rug`](https://crates.io/crates/rug) crate,
//!   ideal for high-precision scientific computing.

pub use crate::core::types::{ComplexValidated, RealValidated};

// Re-export raw traits from core::traits::raw
pub use crate::core::traits::raw::{
    RawComplexTrait, RawRealTrait, RawScalarHyperbolic, RawScalarPow, RawScalarTrait,
    RawScalarTrigonometric,
};

use crate::core::{
    policies::{DebugValidationPolicy, StrictFinitePolicy},
    traits::{
        NumKernel, RawKernel,
        validation::{ValidationPolicyComplex, ValidationPolicyReal},
    },
};
use std::marker::PhantomData;

//------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
/// A strict finite kernel validation policy for raw real numbers.
/// This policy ensures that the real numbers are finite and not NaN.
pub struct NumKernelStrictFinite<RawReal: RawRealTrait, const PRECISION: u32> {
    _phantom: PhantomData<RawReal>,
}

impl<RawReal: RawRealTrait, const PRECISION: u32> RawKernel
    for NumKernelStrictFinite<RawReal, PRECISION>
{
    type RawReal = RawReal;
    type RawComplex = RawReal::RawComplex;
}

impl<RawReal: RawRealTrait, const PRECISION: u32> NumKernel
    for NumKernelStrictFinite<RawReal, PRECISION>
where
    StrictFinitePolicy<RawReal, PRECISION>: ValidationPolicyReal<Value = RawReal>,
    StrictFinitePolicy<RawReal::RawComplex, PRECISION>:
        ValidationPolicyComplex<Value = RawReal::RawComplex>,
{
    type RealPolicy = StrictFinitePolicy<RawReal, PRECISION>;

    type ComplexPolicy = StrictFinitePolicy<RawReal::RawComplex, PRECISION>;

    type Real = RealValidated<Self>;
    type Complex = ComplexValidated<Self>;
}
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------
/// A debug-only strict finite kernel validation policy for raw real numbers.
///
/// This kernel applies [`StrictFinitePolicy`] validation only in debug builds.
/// In release builds, validation is skipped for maximum performance.
///
/// ## Use Cases
///
/// - Performance-critical code where validation overhead is unacceptable in production
/// - Code that has been thoroughly tested with [`NumKernelStrictFinite`] in debug mode
///
/// ## Comparison with `NumKernelStrictFinite`
///
/// | Aspect | `NumKernelStrictFinite` | `NumKernelStrictFiniteInDebug` |
/// |--------|-------------------------|--------------------------------|
/// | Debug validation | ✅ Yes | ✅ Yes |
/// | Release validation | ✅ Yes | ❌ No |
/// | `Eq + Hash + Ord` | ✅ Yes | ❌ No (not safe without validation) |
/// | Performance | Slower | Faster in release |
///
/// ## Safety Note
///
/// Since validation is skipped in release builds, this kernel does NOT implement
/// the [`GuaranteesFiniteRealValues`](crate::core::traits::validation::GuaranteesFiniteRealValues)
/// marker trait, meaning it cannot be used with `HashMap`, `BTreeMap`, or other
/// collections requiring `Eq` + `Hash` + `Ord`.
pub struct NumKernelStrictFiniteInDebug<RawReal: RawRealTrait, const PRECISION: u32> {
    _phantom: PhantomData<RawReal>,
}

impl<RawReal: RawRealTrait, const PRECISION: u32> RawKernel
    for NumKernelStrictFiniteInDebug<RawReal, PRECISION>
{
    type RawReal = RawReal;
    type RawComplex = RawReal::RawComplex;
}

impl<RawReal: RawRealTrait, const PRECISION: u32> NumKernel
    for NumKernelStrictFiniteInDebug<RawReal, PRECISION>
where
    StrictFinitePolicy<RawReal, PRECISION>: ValidationPolicyReal<Value = RawReal>,
    StrictFinitePolicy<RawReal::RawComplex, PRECISION>:
        ValidationPolicyComplex<Value = RawReal::RawComplex>,
{
    type RealPolicy = DebugValidationPolicy<StrictFinitePolicy<RawReal, PRECISION>>;

    type ComplexPolicy = DebugValidationPolicy<StrictFinitePolicy<RawReal::RawComplex, PRECISION>>;

    type Real = RealValidated<Self>;
    type Complex = ComplexValidated<Self>;
}

//-----------------------------------------------------------------------------------------------