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
use crate::FloatError;
use super::{GuardedF32, UnguardedF32};
impl UnguardedF32 {
/// Checks if the `UnguardedF32` value is valid (finite).
///
/// # Returns
///
/// Returns a `GuardedF32` if the value is valid (finite), otherwise returns an error.
///
/// # Errors
///
/// Returns `FloatError` if the value is NaN or infinite.
///
/// # Example
///
/// ```rust
/// use floatguard::{UnguardedF32, FloatError, GuardedF32};
///
/// let unchecked_f32 = UnguardedF32::new(1.0);
/// assert_eq!(unchecked_f32.check(), GuardedF32::new(1.0));
///
/// let invalid_f32 = UnguardedF32::new(f32::NAN);
/// assert_eq!(invalid_f32.check(), Err(FloatError::NaN));
///
/// let inf_f32 = UnguardedF32::new(f32::INFINITY);
/// assert_eq!(inf_f32.check(), Err(FloatError::Infinity));
///
/// let neg_inf_f32 = UnguardedF32::new(f32::NEG_INFINITY);
/// assert_eq!(neg_inf_f32.check(), Err(FloatError::Infinity));
/// ```
pub const fn check(self) -> Result<GuardedF32, FloatError> {
GuardedF32::new(self.0)
}
}
impl TryFrom<UnguardedF32> for GuardedF32 {
type Error = FloatError;
/// Converts an `UnguardedF32` to `GuardedF32`.
///
/// # Returns
///
/// Returns a `GuardedF32` if the value is valid (finite), otherwise returns an error.
///
/// # Errors
///
/// Returns `FloatError` if the value is NaN or infinite.
///
/// # Example
///
/// ```rust
/// use floatguard::{UnguardedF32, FloatError, GuardedF32};
///
/// let valid_value = UnguardedF32::new(2.0);
/// assert_eq!(valid_value.try_into(), GuardedF32::new(2.0));
///
/// let invalid_value = UnguardedF32::new(f32::NAN);
/// assert_eq!(GuardedF32::try_from(invalid_value), Err(FloatError::NaN));
///
/// let inf_value = UnguardedF32::new(f32::INFINITY);
/// assert_eq!(GuardedF32::try_from(inf_value), Err(FloatError::Infinity));
/// ```
fn try_from(value: UnguardedF32) -> Result<Self, Self::Error> {
value.check()
}
}
impl From<GuardedF32> for UnguardedF32 {
/// Converts a `GuardedF32` into an `UnguardedF32`.
///
/// # Returns
///
/// Returns a new `UnguardedF32` instance containing the inner `f32` value.
///
/// # Example
///
/// ```rust
/// use floatguard::{UnguardedF32, GuardedF32};
///
/// let checked_f32 = GuardedF32::new(3.14).unwrap();
/// let unchecked_f32 = UnguardedF32::from(checked_f32);
/// assert_eq!(unchecked_f32.check(), GuardedF32::new(3.14));
/// ```
fn from(value: GuardedF32) -> Self {
Self(value.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::GuardedF32;
use crate::f32::tests::{invalid_f32, valid_f32};
use proptest::prelude::*;
proptest! {
#[test]
fn test_from_valid(a in valid_f32()) {
let checked_a = UnguardedF32::new(a);
prop_assert_eq!(checked_a.check(), GuardedF32::new(a));
prop_assert_eq!(f32::try_from(checked_a), Ok(a));
}
#[test]
fn test_from_invalid(a in invalid_f32()) {
let checked_a = UnguardedF32::new(a);
let float_error = if a.is_nan() {
FloatError::NaN
} else if a.is_infinite() {
FloatError::Infinity
} else {
unreachable!()
};
prop_assert_eq!(checked_a.check(), Err(float_error));
prop_assert_eq!(f32::try_from(checked_a), Err(float_error));
}
#[test]
fn test_from_guarded(guarded in valid_f32()) {
let guarded_f32 = GuardedF32::new(guarded).unwrap();
let unchecked_f32: UnguardedF32 = UnguardedF32::from(guarded_f32);
prop_assert_eq!(GuardedF32::try_from(unchecked_f32), Ok(guarded_f32));
}
}
}