ScalarCore

Trait Alias ScalarCore 

Source
trait ScalarCore = Sized
    + Clone
    + Debug
    + Display
    + PartialEq
    + Send
    + Sync
    + Zero
    + One
    + Serialize
    + DeserializeOwned
    + 'static;
Expand description

Fundamental trait alias bundling 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 alias 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
  • Zero: Has an additive identity element (0)
  • One: Has a multiplicative identity element (1)
  • Serialize: Can be serialized (via serde)
  • DeserializeOwned: Can be deserialized without borrowing (via serde)
  • 'static: Contains no non-static references

§Design Rationale

This trait alias 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 trait alias:
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 try_create::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

This is a trait alias, not a regular trait. It cannot be implemented directly; instead, types must implement all the constituent traits individually. The trait alias simply provides a convenient shorthand for referring to this specific combination of traits.

§See Also