path_traits/scalar.rs
1//! Scalar type alias for arc-length and parameter computations.
2//!
3//! This module defines the [`Scalar`] trait, a numeric trait with additional bounds
4//! (`Debug`, `Copy`, `'static`) required for arc-length and parameter computations
5//! throughout the crate. The supertraits vary by feature configuration.
6
7#[cfg(all(feature = "num-traits", feature = "std"))]
8mod inner {
9 use num_traits::Float;
10
11 /// A scalar type suitable for arc-length, parameter, and distance computations.
12 ///
13 /// Requires `Float` from `num-traits` (with `std`) plus `Debug`, `Copy`, and `'static` bounds.
14 /// Implementations are provided automatically for any type satisfying those bounds
15 /// (e.g. `f32`, `f64`).
16 pub trait Scalar: Float + core::fmt::Debug + Copy + 'static {
17 /// Returns the additive identity (0.0).
18 fn zero() -> Self;
19 /// Returns the multiplicative identity (1.0).
20 fn one() -> Self;
21 /// Cast a `usize` value to this scalar type.
22 fn from_usize(n: usize) -> Self;
23 }
24
25 impl<T: Float + core::fmt::Debug + Copy + 'static> Scalar for T {
26 fn zero() -> Self {
27 T::zero()
28 }
29 fn one() -> Self {
30 T::one()
31 }
32 fn from_usize(n: usize) -> Self {
33 num_traits::NumCast::from(n).unwrap_or(Self::one())
34 }
35 }
36}
37
38#[cfg(all(feature = "num-traits", not(feature = "std")))]
39mod inner {
40 use num_traits::float::FloatCore;
41
42 /// A scalar type suitable for arc-length, parameter, and distance computations.
43 ///
44 /// Requires `FloatCore` from `num-traits` plus `Debug`, `Copy`, and `'static` bounds.
45 /// Implementations are provided automatically for any type satisfying those bounds
46 /// (e.g. `f32`, `f64`).
47 pub trait Scalar: FloatCore + core::fmt::Debug + Copy + 'static {
48 /// Returns the additive identity (0.0).
49 fn zero() -> Self;
50 /// Returns the multiplicative identity (1.0).
51 fn one() -> Self;
52 /// Cast a `usize` value to this scalar type.
53 fn from_usize(n: usize) -> Self;
54 }
55
56 impl<T: FloatCore + core::fmt::Debug + Copy + 'static> Scalar for T {
57 fn zero() -> Self {
58 T::zero()
59 }
60 fn one() -> Self {
61 T::one()
62 }
63 fn from_usize(n: usize) -> Self {
64 num_traits::NumCast::from(n).unwrap_or(Self::one())
65 }
66 }
67}
68
69#[cfg(not(feature = "num-traits"))]
70mod inner {
71 /// A scalar type suitable for arc-length, parameter, and distance computations.
72 ///
73 /// Requires arithmetic operations (`Add`, `Sub`, `Mul`, `Div`, `Neg`),
74 /// `PartialOrd`, `Debug`, `Copy`, and `'static` bounds.
75 /// Manual implementations are provided for `f32` and `f64`.
76 pub trait Scalar:
77 core::ops::Add<Output = Self>
78 + core::ops::Sub<Output = Self>
79 + core::ops::Mul<Output = Self>
80 + core::ops::Div<Output = Self>
81 + core::ops::Neg<Output = Self>
82 + PartialOrd
83 + core::fmt::Debug
84 + Copy
85 + 'static
86 {
87 /// Returns the additive identity (0.0).
88 fn zero() -> Self;
89 /// Returns the multiplicative identity (1.0).
90 fn one() -> Self;
91 /// Cast a `usize` value to this scalar type.
92 fn from_usize(n: usize) -> Self;
93 }
94
95 macro_rules! impl_scalar {
96 ($($t:ty),+) => {
97 $(
98 impl Scalar for $t {
99 fn zero() -> Self { 0.0 }
100 fn one() -> Self { 1.0 }
101 fn from_usize(n: usize) -> Self { n as $t }
102 }
103 )+
104 };
105 }
106
107 impl_scalar!(f32, f64);
108}
109
110pub use inner::Scalar;