• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

facet-rs / facet / 15787660308

20 Jun 2025 08:54PM UTC coverage: 61.487% (+1.8%) from 59.711%
15787660308

Pull #774

github

web-flow
Merge 8921ab663 into ec93ce66e
Pull Request #774: split up Partial::end into smaller bits

147 of 165 new or added lines in 2 files covered. (89.09%)

1817 existing lines in 50 files now uncovered.

10564 of 17181 relevant lines covered (61.49%)

161.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.0
/facet-core/src/impls_ordered_float.rs
1
use crate::{
2
    Def, Facet, PtrConst, PtrMut, PtrUninit, Repr, Shape, StructType, TryBorrowInnerError,
3
    TryFromError, TryIntoInnerError, Type, UserType, ValueVTable, field_in_type, value_vtable,
4
};
5
use ordered_float::{NotNan, OrderedFloat};
6

7
macro_rules! impl_facet_for_ordered_float_and_notnan {
8
    ($float:ty) => {
9
        unsafe impl<'a> Facet<'a> for OrderedFloat<$float> {
10
            const VTABLE: &'static ValueVTable = &const {
11
                // Define conversion functions for transparency
12
                unsafe fn try_from<'shape, 'dst>(
3✔
13
                    src_ptr: PtrConst<'_>,
3✔
14
                    src_shape: &'shape Shape<'shape>,
3✔
15
                    dst: PtrUninit<'dst>,
3✔
16
                ) -> Result<PtrMut<'dst>, TryFromError<'shape>> {
3✔
17
                    if src_shape == <$float as Facet>::SHAPE {
3✔
18
                        // Get the inner value and wrap as OrderedFloat
19
                        let value = unsafe { src_ptr.get::<$float>() };
2✔
20
                        let ord = OrderedFloat(*value);
2✔
21
                        Ok(unsafe { dst.put(ord) })
2✔
22
                    } else {
UNCOV
23
                        let inner_try_from =
×
24
                            (<$float as Facet>::SHAPE.vtable.sized().unwrap().try_from)().ok_or(
1✔
25
                                TryFromError::UnsupportedSourceShape {
1✔
26
                                    src_shape,
1✔
27
                                    expected: &[<$float as Facet>::SHAPE],
1✔
28
                                },
1✔
29
                            )?;
1✔
30
                        // fallback to inner's try_from
31
                        // This relies on the fact that `dst` is the same size as `OrderedFloat<$float>`
32
                        // which should be true because `OrderedFloat` is `repr(transparent)`
UNCOV
33
                        let inner_result = unsafe { (inner_try_from)(src_ptr, src_shape, dst) };
×
34
                        match inner_result {
×
35
                            Ok(result) => {
×
36
                                // After conversion to inner type, wrap as OrderedFloat
UNCOV
37
                                let value = unsafe { result.read::<$float>() };
×
38
                                let ord = OrderedFloat(value);
×
39
                                Ok(unsafe { dst.put(ord) })
×
40
                            }
UNCOV
41
                            Err(e) => Err(e),
×
42
                        }
43
                    }
44
                }
3✔
45

46
                // Conversion back to inner float type
UNCOV
47
                unsafe fn try_into_inner<'dst>(
×
48
                    src_ptr: PtrMut<'_>,
×
49
                    dst: PtrUninit<'dst>,
×
50
                ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
×
51
                    let v = unsafe { src_ptr.read::<OrderedFloat<$float>>() };
×
52
                    Ok(unsafe { dst.put(v.0) })
×
53
                }
×
54

55
                // Borrow inner float type
UNCOV
56
                unsafe fn try_borrow_inner(
×
57
                    src_ptr: PtrConst<'_>,
×
58
                ) -> Result<PtrConst<'_>, TryBorrowInnerError> {
×
59
                    let v = unsafe { src_ptr.get::<OrderedFloat<$float>>() };
×
60
                    Ok(PtrConst::new((&v.0) as *const $float as *const u8))
×
61
                }
×
62

63
                let mut vtable =
64
                    value_vtable!((), |f, _opts| write!(f, "{}", Self::SHAPE.type_identifier));
5✔
65
                {
66
                    let vtable = vtable.sized_mut().unwrap();
UNCOV
67
                    vtable.parse = || {
×
68
                        // `OrderedFloat` is `repr(transparent)`
UNCOV
69
                        (<$float as Facet>::SHAPE.vtable.sized().unwrap().parse)()
×
70
                    };
×
71
                    vtable.try_from = || Some(try_from);
7✔
UNCOV
72
                    vtable.try_into_inner = || Some(try_into_inner);
×
73
                    vtable.try_borrow_inner = || Some(try_borrow_inner);
×
74
                }
75
                vtable
76
            };
77

78
            const SHAPE: &'static Shape<'static> = &const {
79
                fn inner_shape() -> &'static Shape<'static> {
6✔
80
                    <$float as Facet>::SHAPE
6✔
81
                }
6✔
82

83
                Shape::builder_for_sized::<Self>()
84
                    .type_identifier("OrderedFloat")
85
                    .ty(Type::User(UserType::Struct(
86
                        StructType::builder()
87
                            .repr(Repr::transparent())
88
                            .fields(&const { [field_in_type!(Self, 0)] })
89
                            .kind(crate::StructKind::Tuple)
90
                            .build(),
91
                    )))
92
                    .def(Def::Scalar)
93
                    .inner(inner_shape)
94
                    .build()
95
            };
96
        }
97

98
        unsafe impl<'a> Facet<'a> for NotNan<$float> {
99
            const VTABLE: &'static ValueVTable = &const {
100
                // Conversion from inner float type to NotNan<$float>
UNCOV
101
                unsafe fn try_from<'shape, 'dst>(
×
UNCOV
102
                    src_ptr: PtrConst<'_>,
×
UNCOV
103
                    src_shape: &'shape Shape<'shape>,
×
UNCOV
104
                    dst: PtrUninit<'dst>,
×
UNCOV
105
                ) -> Result<PtrMut<'dst>, TryFromError<'shape>> {
×
UNCOV
106
                    if src_shape == <$float as Facet>::SHAPE {
×
107
                        // Get the inner value and check that it's not NaN
108
                        let value = unsafe { *src_ptr.get::<$float>() };
×
109
                        let nn =
×
110
                            NotNan::new(value).map_err(|_| TryFromError::Generic("was NaN"))?;
×
111
                        Ok(unsafe { dst.put(nn) })
×
112
                    } else {
UNCOV
113
                        let inner_try_from =
×
114
                            (<$float as Facet>::SHAPE.vtable.sized().unwrap().try_from)().ok_or(
×
115
                                TryFromError::UnsupportedSourceShape {
×
116
                                    src_shape,
×
117
                                    expected: &[<$float as Facet>::SHAPE],
×
UNCOV
118
                                },
×
119
                            )?;
×
120

121
                        // fallback to inner's try_from
122
                        // This relies on the fact that `dst` is the same size as `NotNan<$float>`
123
                        // which should be true because `NotNan` is `repr(transparent)`
124
                        let inner_result = unsafe { (inner_try_from)(src_ptr, src_shape, dst) };
×
125
                        match inner_result {
×
UNCOV
126
                            Ok(result) => {
×
127
                                // After conversion to inner type, wrap as NotNan
UNCOV
128
                                let value = unsafe { *result.get::<$float>() };
×
UNCOV
129
                                let nn = NotNan::new(value)
×
130
                                    .map_err(|_| TryFromError::Generic("was NaN"))?;
×
131
                                Ok(unsafe { dst.put(nn) })
×
132
                            }
UNCOV
133
                            Err(e) => Err(e),
×
134
                        }
135
                    }
136
                }
×
137

138
                // Conversion back to inner float type
139
                unsafe fn try_into_inner<'dst>(
×
UNCOV
140
                    src_ptr: PtrMut<'_>,
×
UNCOV
141
                    dst: PtrUninit<'dst>,
×
142
                ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
×
UNCOV
143
                    let v = unsafe { src_ptr.read::<NotNan<$float>>() };
×
UNCOV
144
                    Ok(unsafe { dst.put(v.into_inner()) })
×
145
                }
×
146

147
                // Borrow inner float type
148
                unsafe fn try_borrow_inner(
×
149
                    src_ptr: PtrConst<'_>,
×
150
                ) -> Result<PtrConst<'_>, TryBorrowInnerError> {
×
151
                    let v = unsafe { src_ptr.get::<NotNan<$float>>() };
×
UNCOV
152
                    Ok(PtrConst::new(
×
UNCOV
153
                        (&v.into_inner()) as *const $float as *const u8,
×
154
                    ))
×
155
                }
×
156

157
                let mut vtable =
158
                    value_vtable!((), |f, _opts| write!(f, "{}", Self::SHAPE.type_identifier));
×
159
                // Accept parsing as inner T, but enforce NotNan invariant
160
                {
161
                    let vtable = vtable.sized_mut().unwrap();
UNCOV
162
                    vtable.parse = || {
×
UNCOV
163
                        Some(|s, target| match s.parse::<$float>() {
×
164
                            Ok(inner) => match NotNan::new(inner) {
×
UNCOV
165
                                Ok(not_nan) => Ok(unsafe { target.put(not_nan) }),
×
166
                                Err(_) => {
UNCOV
167
                                    Err(crate::ParseError::Generic("NaN is not allowed for NotNan"))
×
168
                                }
169
                            },
170
                            Err(_) => Err(crate::ParseError::Generic(
×
171
                                "Failed to parse inner type for NotNan",
×
UNCOV
172
                            )),
×
173
                        })
×
UNCOV
174
                    };
×
UNCOV
175
                    vtable.try_from = || Some(try_from);
×
176
                    vtable.try_into_inner = || Some(try_into_inner);
×
177
                    vtable.try_borrow_inner = || Some(try_borrow_inner);
×
178
                }
179
                vtable
180
            };
181

182
            const SHAPE: &'static Shape<'static> = &const {
183
                fn inner_shape() -> &'static Shape<'static> {
×
UNCOV
184
                    <$float as Facet>::SHAPE
×
UNCOV
185
                }
×
186

187
                Shape::builder_for_sized::<Self>()
188
                    .type_identifier("NotNan")
189
                    .ty(Type::User(UserType::Opaque))
190
                    .def(Def::Scalar)
191
                    .inner(inner_shape)
192
                    .build()
193
            };
194
        }
195
    };
196
}
197

198
impl_facet_for_ordered_float_and_notnan!(f32);
199
impl_facet_for_ordered_float_and_notnan!(f64);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc