Skip to main content

ScalarCore

Trait ScalarCore 

Source
pub trait ScalarCore:
    Sized
    + Clone
    + Debug
    + Display
    + PartialEq
    + Send
    + Sync
    + Serialize
    + for<'a> Deserialize<'a>
    + 'static {
    type RawScalarType: RawScalarTrait;

    // Required methods
    fn as_raw_scalar_ref(&self) -> &Self::RawScalarType;
    fn into_raw_scalar(self) -> Self::RawScalarType;

    // Provided method
    fn eq_raw(&self, other: &Self::RawScalarType) -> bool { ... }
}
Expand description

Fundamental trait defining core requirements for all scalar types.

ScalarCore aggregates the essential traits that every scalar type in num-valid must implement, regardless of whether it’s real or complex, native or arbitrary-precision. This trait provides a single, maintainable definition of the foundational capabilities required by all floating-point scalar types.

§Included Traits

  • Sized: Types must have a known size at compile time
  • Clone: Values can be duplicated (note: not necessarily Copy)
  • Debug: Supports formatted debugging output with {:?}
  • Display: Supports user-facing formatted output with {}
  • PartialEq: Supports equality comparisons (may upgrade to Eq with finite guarantees)
  • Send: Safe to transfer ownership across thread boundaries
  • Sync: Safe to share references across threads
  • Serialize: Can be serialized (via serde)
  • Deserialize: Can be deserialized (via serde)
  • 'static: Contains no non-static references

§Design Rationale

This trait serves several important purposes:

  1. Single Source of Truth: Defines core requirements in one place, reducing duplication
  2. Maintainability: Changes to fundamental requirements only need to be made once
  3. Readability: Simplifies trait bounds in FpScalar and other high-level traits
  4. Consistency: Ensures all scalar types share the same baseline capabilities

§Usage in Type Bounds

ScalarCore is primarily used as a super-trait bound in FpScalar:

// Instead of listing all traits individually:
// pub trait FpScalar: Sized + Clone + Debug + Display + PartialEq + Send + Sync + ...

// We use the core trait:
pub trait FpScalar: ScalarCore + /* other mathematical traits */ {
    // ...
}

§Thread Safety

The inclusion of Send and Sync makes all scalar types safe for concurrent use:

  • Send: Scalars can be moved between threads
  • Sync: Scalars can be shared via &T between threads

This enables parallel numerical computations without additional synchronization overhead.

§Serialization Support

All scalar types support serialization through serde:

ⓘ
use num_valid::RealNative64StrictFinite;
use num_valid::TryNew;
use serde_json;

let value = RealNative64StrictFinite::try_new(3.14159).unwrap();

// Serialize to JSON
let json = serde_json::to_string(&value).unwrap();
assert_eq!(json, "3.14159");

// Deserialize from JSON
let deserialized: RealNative64StrictFinite = serde_json::from_str(&json).unwrap();
assert_eq!(value, deserialized);

§Relationship to Other Traits

§Implementation Note

Implementors define the raw scalar representation through ScalarCore::RawScalarType and provide borrowed and owned access to it through ScalarCore::as_raw_scalar_ref and ScalarCore::into_raw_scalar.

Some wrapper types (e.g. in scalars) additionally derive AsRef/IntoInner. Those only peel off one layer of wrapping and return the next inner type (which may itself still be a validated scalar), whereas as_raw_scalar_ref/into_raw_scalar always unwrap all the way down to the underlying ScalarCore::RawScalarType (e.g. f64), regardless of how many layers of wrapping separate the two.

§See Also

Required Associated Types§

Source

type RawScalarType: RawScalarTrait

The type of the inner value that will be extracted.

Required Methods§

Source

fn as_raw_scalar_ref(&self) -> &Self::RawScalarType

Returns a reference to the inner value.

Source

fn into_raw_scalar(self) -> Self::RawScalarType

Consumes the scalar and returns its inner raw scalar value.

§Example
ⓘ
let value: T = ...;
let raw: T::RawScalarType = value.into_raw_scalar();

Provided Methods§

Source

fn eq_raw(&self, other: &Self::RawScalarType) -> bool

Compares this scalar with a value in its underlying raw representation.

use num_valid::{RealNative64StrictFinite, ScalarCore};
use num_valid::TryNew;

let value = RealNative64StrictFinite::try_new(3.5).unwrap();
assert!(value.eq_raw(&3.5));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl ScalarCore for Complex

Source§

impl ScalarCore for Float

Source§

impl ScalarCore for f32

Source§

impl ScalarCore for f64

Source§

impl<T: NativeReal> ScalarCore for Complex<T>

Implementors§