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
//! Core traits for type-safe numeric wrappers.
//!
//! This module defines the fundamental traits that enable type-safe arithmetic
//! and bounds checking for numeric wrapper types.
use RangeInclusive;
/// Generic trait for creating newtype wrappers around numeric types.
///
/// This trait provides the foundation for type-safe arithmetic by wrapping
/// primitive numeric types (u64, i64, i128) in strongly-typed structs.
///
/// # Type Safety
///
/// By implementing this trait for different wrapper types, we ensure that
/// only compatible types can be used in arithmetic operations, preventing
/// dimensional errors at compile time.
///
/// # Example
///
/// ```ignore
/// struct Price { inner: u64 }
/// impl WrapperNum<u64> for Price {
/// type Inner = u64;
/// fn new(value: u64) -> Self { Price { inner: value } }
/// fn as_inner(&self) -> u64 { self.inner }
/// }
/// ```
/// Trait for enforcing value bounds on numeric wrapper types.
///
/// This trait enables compile-time and runtime bounds checking for types
/// that need to restrict their valid value ranges. This is crucial for
/// preventing overflow errors and ensuring data integrity.
///
/// # Overflow Prevention
///
/// By defining upper bounds smaller than the underlying type's maximum,
/// we can catch potential overflows before they occur:
///
/// ```ignore
/// // BaseLots is limited to u32::MAX even though it wraps u64
/// impl ScalarBounds<u64> for BaseLots {
/// const LOWER_BOUND: u64 = 0;
/// const UPPER_BOUND: u64 = u32::MAX as u64;
/// }
///
/// let valid = BaseLots::new(1000);
/// assert!(valid.is_in_bounds());
///
/// let too_large = BaseLots::new(u64::MAX);
/// assert!(!too_large.is_in_bounds()); // Caught before overflow!
/// ```