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

facet-rs / facet / 16482855623

23 Jul 2025 10:08PM UTC coverage: 58.447% (-0.2%) from 58.68%
16482855623

Pull #855

github

web-flow
Merge dca4c2302 into 5e8e214d1
Pull Request #855: wip: Remove 'shape lifetime

400 of 572 new or added lines in 70 files covered. (69.93%)

3 existing lines in 3 files now uncovered.

11939 of 20427 relevant lines covered (58.45%)

120.58 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<'dst>(
3✔
13
                    src_ptr: PtrConst<'_>,
3✔
14
                    src_shape: &'static Shape,
3✔
15
                    dst: PtrUninit<'dst>,
3✔
16
                ) -> Result<PtrMut<'dst>, TryFromError> {
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 {
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)`
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
37
                                let value = unsafe { result.read::<$float>() };
×
38
                                let ord = OrderedFloat(value);
×
39
                                Ok(unsafe { dst.put(ord) })
×
40
                            }
41
                            Err(e) => Err(e),
×
42
                        }
43
                    }
44
                }
3✔
45

46
                // Conversion back to inner float type
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
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));
7✔
65
                {
66
                    let vtable = vtable.sized_mut().unwrap();
67
                    vtable.parse = || {
×
68
                        // `OrderedFloat` is `repr(transparent)`
69
                        (<$float as Facet>::SHAPE.vtable.sized().unwrap().parse)()
×
70
                    };
×
71
                    vtable.try_from = || Some(try_from);
7✔
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 = &const {
79
                fn inner_shape() -> &'static Shape {
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>
NEW
101
                unsafe fn try_from<'dst>(
×
102
                    src_ptr: PtrConst<'_>,
×
NEW
103
                    src_shape: &'static Shape,
×
104
                    dst: PtrUninit<'dst>,
×
NEW
105
                ) -> Result<PtrMut<'dst>, TryFromError> {
×
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 {
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],
×
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 {
×
126
                            Ok(result) => {
×
127
                                // After conversion to inner type, wrap as NotNan
128
                                let value = unsafe { *result.get::<$float>() };
×
129
                                let nn = NotNan::new(value)
×
130
                                    .map_err(|_| TryFromError::Generic("was NaN"))?;
×
131
                                Ok(unsafe { dst.put(nn) })
×
132
                            }
133
                            Err(e) => Err(e),
×
134
                        }
135
                    }
136
                }
×
137

138
                // Conversion back to inner float type
139
                unsafe fn try_into_inner<'dst>(
×
140
                    src_ptr: PtrMut<'_>,
×
141
                    dst: PtrUninit<'dst>,
×
142
                ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
×
143
                    let v = unsafe { src_ptr.read::<NotNan<$float>>() };
×
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>>() };
×
152
                    Ok(PtrConst::new(
×
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();
162
                    vtable.parse = || {
×
163
                        Some(|s, target| match s.parse::<$float>() {
×
164
                            Ok(inner) => match NotNan::new(inner) {
×
165
                                Ok(not_nan) => Ok(unsafe { target.put(not_nan) }),
×
166
                                Err(_) => {
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",
×
172
                            )),
×
173
                        })
×
174
                    };
×
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 = &const {
NEW
183
                fn inner_shape() -> &'static Shape {
×
184
                    <$float as Facet>::SHAPE
×
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