1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! Validation policies for scalar types.
//!
//! This module provides the validation policies that determine how scalar values
//! are checked for validity. The primary policy is `StrictFinitePolicy`, which
//! ensures values are finite and not subnormal.
use crate;
use duplicate_item;
use Complex;
use PhantomData;
use ValidationPolicy;
/// A validation policy that checks for strict finiteness.
///
/// For floating-point types (`ScalarType = `[`f32`], `ScalarType = `[`f64`], `ScalarType = `[`rug::Float`](https://docs.rs/rug/latest/rug/struct.Float.html)), this policy ensures that the value is:
/// - Not NaN (Not a Number).
/// - Not positive or negative Infinity.
/// - Not subnormal (for `f32`/`f64`).
/// While [`rug::Float`](https://docs.rs/rug/latest/rug/struct.Float.html) maintains its specified precision rather than having distinct subnormal
/// representation in the IEEE 754 sense, this policy will also reject [`rug::Float`](https://docs.rs/rug/latest/rug/struct.Float.html) values that are classified as
/// [`FpCategory::Subnormal`](std::num::FpCategory::Subnormal) (e.g., results of underflow that are tiny but not exactly zero).
///
/// For complex types (`ScalarType = `[`Complex<f32>`], `ScalarType = `[`Complex<f64>`], `ScalarType = `[`rug::Complex`](https://docs.rs/rug/latest/rug/struct.Complex.html)), this policy applies
/// the strict finiteness check to both the real and imaginary parts.
///
/// This struct is a Zero-Sized Type (ZST) and uses [`PhantomData`] to associate
/// with the `ScalarType` it validates.
;
/// A type alias for a validation policy that enforces strict finiteness for the raw [`f64`] type.
///
/// This is a convenient alias for [`StrictFinitePolicy<f64, 53>`], configured specifically
/// for Rust's native 64-bit floating-point number. It is used to validate that a given [`f64`]
/// value is suitable for use within the library's validated types.
///
/// This policy ensures that a `f64` value is:
/// - Not `NaN` (Not a Number).
/// - Not positive or negative `Infinity`.
/// - Not a subnormal number.
///
/// It is a fundamental building block for the native `f64` kernel, providing the validation
/// logic for [`RealNative64StrictFinite`](crate::backends::native::validated::RealNative64StrictFinite).
///
/// # Example
///
/// ```rust
/// use num_valid::{
/// core::{
/// errors::ErrorsValidationRawReal,
/// policies::Native64RawRealStrictFinitePolicy,
/// },
/// };
/// use try_create::ValidationPolicy;
///
/// // A valid finite number passes validation.
/// assert!(Native64RawRealStrictFinitePolicy::validate(1.0).is_ok());
///
/// // NaN fails validation.
/// let result_nan = Native64RawRealStrictFinitePolicy::validate(f64::NAN);
/// assert!(matches!(result_nan, Err(ErrorsValidationRawReal::IsNaN { .. })));
///
/// // Infinity fails validation.
/// let result_inf = Native64RawRealStrictFinitePolicy::validate(f64::INFINITY);
/// assert!(matches!(result_inf, Err(ErrorsValidationRawReal::IsPosInfinity { .. })));
/// ```
pub type Native64RawRealStrictFinitePolicy = f64MANTISSA_DIGITS }>;
/// A type alias for a validation policy that enforces strict finiteness for the raw [`num::Complex<f64>`] type.
///
/// This is a convenient alias for [`StrictFinitePolicy<Complex<f64>, 53>`], configured for
/// Rust's native 64-bit complex numbers. It is used to validate that a given [`num::Complex<f64>`]
/// value is suitable for use within the library's validated types.
///
/// This policy applies the strict finiteness check (as defined by [`Native64RawRealStrictFinitePolicy`])
/// to both the real and imaginary parts of the complex number. A [`num::Complex<f64>`] value is considered
/// valid if and only if both of its components are finite (not `NaN`, `Infinity`, or subnormal).
///
/// It is a fundamental building block for the native `f64` kernel, providing the validation
/// logic for [`ComplexNative64StrictFinite`](crate::backends::native::validated::ComplexNative64StrictFinite).
///
/// # Example
///
/// ```rust
/// use num_valid::{
/// core::{
/// errors::{ErrorsValidationRawComplex, ErrorsValidationRawReal},
/// policies::Native64RawComplexStrictFinitePolicy,
/// },
/// };
/// use try_create::ValidationPolicy;
/// use num::Complex;
///
/// // A complex number with valid finite parts passes validation.
/// let valid_complex = Complex::new(1.0, -2.0);
/// assert!(Native64RawComplexStrictFinitePolicy::validate(valid_complex).is_ok());
///
/// // A complex number with NaN in the real part fails validation.
/// let nan_complex = Complex::new(f64::NAN, 2.0);
/// let result_nan = Native64RawComplexStrictFinitePolicy::validate(nan_complex);
/// assert!(matches!(result_nan, Err(ErrorsValidationRawComplex::InvalidRealPart { .. })));
///
/// // A complex number with Infinity in the imaginary part fails validation.
/// let inf_complex = Complex::new(1.0, f64::INFINITY);
/// let result_inf = Native64RawComplexStrictFinitePolicy::validate(inf_complex);
/// assert!(matches!(result_inf, Err(ErrorsValidationRawComplex::InvalidImaginaryPart { .. })));
///
/// // A complex number with invalid parts in both also fails.
/// let both_invalid = Complex::new(f64::NAN, f64::INFINITY);
/// let result_both = Native64RawComplexStrictFinitePolicy::validate(both_invalid);
/// assert!(matches!(result_both, Err(ErrorsValidationRawComplex::InvalidBothParts { .. })));
/// ```
pub type Native64RawComplexStrictFinitePolicy =
f64MANTISSA_DIGITS }>;
/// A type alias for a validation policy that enforces strict finiteness for the raw [`f32`] type.
///
/// This is a convenient alias for [`StrictFinitePolicy<f32, 24>`], configured specifically
/// for Rust's native 32-bit floating-point number. It is used to validate that a given [`f32`]
/// value is suitable for use within the library's validated types.
///
/// This policy ensures that a `f32` value is:
/// - Not `NaN` (Not a Number).
/// - Not positive or negative `Infinity`.
/// - Not a subnormal number.
///
/// It is a fundamental building block for the native `f32` kernel, providing the validation
/// logic for [`RealNative32StrictFinite`](crate::backends::native::validated::RealNative32StrictFinite).
///
/// # Example
///
/// ```rust
/// use num_valid::{
/// core::{
/// errors::ErrorsValidationRawReal,
/// policies::Native32RawRealStrictFinitePolicy,
/// },
/// };
/// use try_create::ValidationPolicy;
///
/// // A valid finite number passes validation.
/// assert!(Native32RawRealStrictFinitePolicy::validate(1.0).is_ok());
///
/// // NaN fails validation.
/// let result_nan = Native32RawRealStrictFinitePolicy::validate(f32::NAN);
/// assert!(matches!(result_nan, Err(ErrorsValidationRawReal::IsNaN { .. })));
///
/// // Infinity fails validation.
/// let result_inf = Native32RawRealStrictFinitePolicy::validate(f32::INFINITY);
/// assert!(matches!(result_inf, Err(ErrorsValidationRawReal::IsPosInfinity { .. })));
/// ```
pub type Native32RawRealStrictFinitePolicy = f32MANTISSA_DIGITS }>;
/// A type alias for a validation policy that enforces strict finiteness for the raw [`num::Complex<f32>`] type.
///
/// This is a convenient alias for [`StrictFinitePolicy<Complex<f32>, 24>`], configured for
/// Rust's native 32-bit complex numbers. It is used to validate that a given [`num::Complex<f32>`]
/// value is suitable for use within the library's validated types.
///
/// This policy applies the strict finiteness check (as defined by [`Native32RawRealStrictFinitePolicy`])
/// to both the real and imaginary parts of the complex number. A [`num::Complex<f32>`] value is considered
/// valid if and only if both of its components are finite (not `NaN`, `Infinity`, or subnormal).
///
/// It is a fundamental building block for the native `f32` kernel, providing the validation
/// logic for [`ComplexNative32StrictFinite`](crate::backends::native::validated::ComplexNative32StrictFinite).
///
/// # Example
///
/// ```rust
/// use num_valid::{
/// core::{
/// errors::{ErrorsValidationRawComplex, ErrorsValidationRawReal},
/// policies::Native32RawComplexStrictFinitePolicy,
/// },
/// };
/// use try_create::ValidationPolicy;
/// use num::Complex;
///
/// // A complex number with valid finite parts passes validation.
/// let valid_complex = Complex::new(1.0, -2.0);
/// assert!(Native32RawComplexStrictFinitePolicy::validate(valid_complex).is_ok());
///
/// // A complex number with NaN in the real part fails validation.
/// let nan_complex = Complex::new(f32::NAN, 2.0);
/// let result_nan = Native32RawComplexStrictFinitePolicy::validate(nan_complex);
/// assert!(matches!(result_nan, Err(ErrorsValidationRawComplex::InvalidRealPart { .. })));
///
/// // A complex number with Infinity in the imaginary part fails validation.
/// let inf_complex = Complex::new(1.0, f32::INFINITY);
/// let result_inf = Native32RawComplexStrictFinitePolicy::validate(inf_complex);
/// assert!(matches!(result_inf, Err(ErrorsValidationRawComplex::InvalidImaginaryPart { .. })));
///
/// // A complex number with invalid parts in both also fails.
/// let both_invalid = Complex::new(f32::NAN, f32::INFINITY);
/// let result_both = Native32RawComplexStrictFinitePolicy::validate(both_invalid);
/// assert!(matches!(result_both, Err(ErrorsValidationRawComplex::InvalidBothParts { .. })));
/// ```
pub type Native32RawComplexStrictFinitePolicy =
f32MANTISSA_DIGITS }>;
/// Ensures the `f32`/`f64` value is strictly finite.
///
/// This policy checks if the `f32`/`f64` value is:
/// - Not NaN (Not a Number).
/// - Not positive or negative Infinity.
/// - Not subnormal.
///
/// # Errors
///
/// Returns [`ErrorsValidationRawReal`] if the value
/// fails any of these checks.
//------------------------------------------------------------------------------------------------
/// A validation policy that is active only in debug builds.
///
/// This struct acts as a wrapper around another [`ValidationPolicy`].
///
/// - In **debug builds** (`#[cfg(debug_assertions)]`), it delegates validation
/// directly to the inner policy `P`.
/// - In **release builds** (`#[cfg(not(debug_assertions))]`), it becomes a "no-op"
/// (no operation), meaning its `validate` and `validate_ref` methods do nothing
/// and always return `Ok`.
///
/// This is useful for enforcing strict, potentially expensive validations during
/// development and testing, while eliminating their performance overhead in production code.
///
/// # Generic Parameters
///
/// - `P`: The inner [`ValidationPolicy`] to use during debug builds.
///
/// # Example
///
/// ```rust,ignore
/// use num_valid::{validation::{DebugValidationPolicy, StrictFinitePolicy, Native64RawRealStrictFinitePolicy}};
/// use try_create::ValidationPolicy;
///
/// // Define a policy that uses Native64RawRealStrictFinitePolicy
/// // (i.e. StrictFinitePolicy<f64, 53>) only in debug builds.
/// type MyPolicy = DebugValidationPolicy<Native64RawRealStrictFinitePolicy>;
///
/// let nan_value = f64::NAN;
/// let result = MyPolicy::validate(nan_value);
///
/// #[cfg(debug_assertions)]
/// {
/// // In debug mode, the validation fails because StrictFinitePolicy catches NaN.
/// assert!(result.is_err());
/// }
///
/// #[cfg(not(debug_assertions))]
/// {
/// // In release mode, the validation is skipped and always succeeds.
/// assert!(result.is_ok());
/// }
/// ```
;
//------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------
/// Validates a complex number by checking both its real and imaginary parts.
///
/// This function uses the provided real validation function `validation_func_raw_real` to
/// validate both parts.
/// If both parts are valid, it returns `Ok(())`. If either part is invalid,
/// it returns an appropriate error variant from the `ErrorsValidationRawComplex` enum.
///
/// # Errors
/// Returns [`ErrorsValidationRawComplex<P::Error>`](ErrorsValidationRawComplex)
/// if either the real part, the imaginary part, or both parts fail validation
/// using the real validation function `validation_func_raw_real`.
pub
//------------------------------------------------------------------------------------------------
//-------------------------------------------------------------
/// Validates a value using the given policy, but only in debug builds.
/// In release builds, this function is a no-op and should be optimized away.
///
/// # Panics
///
/// In debug builds, this function will panic if `P::validate_ref(value)` returns an `Err`.
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
/// Ensures both the real and imaginary parts of a `Complex<f64>` value are strictly finite.
///
/// This policy applies the [`StrictFinitePolicy<f64>`](StrictFinitePolicy) to both
/// the real and imaginary components of the complex number.
///
/// # Errors
///
/// Returns [`ErrorsValidationRawComplex<ErrorsValidationRawReal<f64>>`](ErrorsValidationRawComplex)
/// if either the real part, the imaginary part, or both parts fail the
/// `StrictFinitePolicy<f64>` checks.
//--------------------------------------------------------------------------------------------------
// Rug Implementations (conditional compilation)
//--------------------------------------------------------------------------------------------------
pub
//-------------------------------------------------------------
// Implement the marker for `StrictFinitePolicy` for any real scalar type.
// Implement the marker for `DebugValidationPolicy<StrictFinitePolicy>` for any real scalar type.
// Implement the marker for `StrictFinitePolicy` for any complex scalar type.
// Implement the marker for `DebugValidationPolicy<StrictFinitePolicy>` for any complex scalar type.
//-------------------------------------------------------------