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 timeClone: Values can be duplicated (note: not necessarilyCopy)Debug: Supports formatted debugging output with{:?}Display: Supports user-facing formatted output with{}PartialEq: Supports equality comparisons (may upgrade toEqwith finite guarantees)Send: Safe to transfer ownership across thread boundariesSync: Safe to share references across threadsSerialize: Can be serialized (viaserde)Deserialize: Can be deserialized (viaserde)'static: Contains no non-static references
§Design Rationale
This trait serves several important purposes:
- Single Source of Truth: Defines core requirements in one place, reducing duplication
- Maintainability: Changes to fundamental requirements only need to be made once
- Readability: Simplifies trait bounds in
FpScalarand other high-level traits - 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:
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
FpScalar: ExtendsScalarCorewith mathematical operations and floating-point checksRealScalar: Further specializesFpScalarfor real numbersComplexScalar: Further specializesFpScalarfor complex numbers
§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
FpScalar: The main scalar trait that usesScalarCoreas a foundationRealScalar: Specialized trait for real number typesComplexScalar: Specialized trait for complex number types
Required Associated Types§
Sourcetype RawScalarType: RawScalarTrait
type RawScalarType: RawScalarTrait
The type of the inner value that will be extracted.
Required Methods§
Sourcefn as_raw_scalar_ref(&self) -> &Self::RawScalarType
fn as_raw_scalar_ref(&self) -> &Self::RawScalarType
Returns a reference to the inner value.
Sourcefn into_raw_scalar(self) -> Self::RawScalarType
fn into_raw_scalar(self) -> Self::RawScalarType
Provided Methods§
Sourcefn eq_raw(&self, other: &Self::RawScalarType) -> bool
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".