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

facet-rs / facet / 15200164445

23 May 2025 01:21AM UTC coverage: 57.288% (+0.1%) from 57.185%
15200164445

Pull #666

github

web-flow
Merge 4232978c5 into 4b41e5c8a
Pull Request #666: Add indirection to vtable fns to fix cyclic types

715 of 1482 new or added lines in 34 files covered. (48.25%)

14 existing lines in 5 files now uncovered.

9747 of 17014 relevant lines covered (57.29%)

132.73 hits per line

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

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

8
macro_rules! impl_facet_for_ordered_float_and_notnan {
9
    ($float:ty) => {
10
        unsafe impl<'a> Facet<'a> for OrderedFloat<$float> {
11
            const VTABLE: &'static ValueVTable = &const {
12
                // Define conversion functions for transparency
13
                unsafe fn try_from<'shape, 'dst>(
1✔
14
                    src_ptr: PtrConst<'_>,
1✔
15
                    src_shape: &'shape Shape<'shape>,
1✔
16
                    dst: PtrUninit<'dst>,
1✔
17
                ) -> Result<PtrMut<'dst>, TryFromError<'shape>> {
1✔
18
                    if src_shape == <$float as Facet>::SHAPE {
1✔
19
                        // Get the inner value and wrap as OrderedFloat
20
                        let value = unsafe { src_ptr.get::<$float>() };
×
21
                        let ord = OrderedFloat(*value);
×
22
                        Ok(unsafe { dst.put(ord) })
×
23
                    } else {
24
                        let inner_try_from = (<$float as Facet>::SHAPE.vtable.try_from)().ok_or(
1✔
25
                            TryFromError::UnsupportedSourceShape {
1✔
26
                                src_shape,
1✔
27
                                expected: &[<$float as Facet>::SHAPE],
1✔
28
                            },
1✔
29
                        )?;
×
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) };
1✔
34
                        match inner_result {
1✔
35
                            Ok(result) => {
1✔
36
                                // After conversion to inner type, wrap as OrderedFloat
37
                                let value = unsafe { result.read::<$float>() };
1✔
38
                                let ord = OrderedFloat(value);
1✔
39
                                Ok(unsafe { dst.put(ord) })
1✔
40
                            }
41
                            Err(e) => Err(e),
×
42
                        }
43
                    }
44
                }
1✔
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 = value_vtable!((), |f, _opts| write!(f, "OrderedFloat"));
1✔
NEW
64
                vtable.parse = || {
×
65
                    // `OrderedFloat` is `repr(transparent)`
NEW
66
                    (<$float as Facet>::SHAPE.vtable.parse)()
×
NEW
67
                };
×
68
                vtable.try_from = || Some(try_from);
1✔
NEW
69
                vtable.try_into_inner = || Some(try_into_inner);
×
NEW
70
                vtable.try_borrow_inner = || Some(try_borrow_inner);
×
71
                vtable
72
            };
73

74
            const SHAPE: &'static Shape<'static> = &const {
75
                fn inner_shape() -> &'static Shape<'static> {
2✔
76
                    <$float as Facet>::SHAPE
2✔
77
                }
2✔
78

79
                Shape::builder_for_sized::<Self>()
80
                    .ty(Type::User(UserType::Struct(
81
                        StructType::builder()
82
                            .repr(Repr::transparent())
83
                            .fields(&const { [field_in_type!(Self, 0)] })
84
                            .kind(crate::StructKind::Tuple)
85
                            .build(),
86
                    )))
87
                    .def(Def::Scalar(
88
                        ScalarDef::builder()
89
                            // Affinity: use number affinity as inner's
90
                            .affinity(&const { ScalarAffinity::opaque().build() })
91
                            .build(),
92
                    ))
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>
101
                unsafe fn try_from<'shape, 'dst>(
×
102
                    src_ptr: PtrConst<'_>,
×
103
                    src_shape: &'shape Shape<'shape>,
×
104
                    dst: PtrUninit<'dst>,
×
105
                ) -> Result<PtrMut<'dst>, TryFromError<'shape>> {
×
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 {
NEW
113
                        let inner_try_from = (<$float as Facet>::SHAPE.vtable.try_from)().ok_or(
×
114
                            TryFromError::UnsupportedSourceShape {
×
115
                                src_shape,
×
116
                                expected: &[<$float as Facet>::SHAPE],
×
117
                            },
×
118
                        )?;
×
119

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

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

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

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

177
            const SHAPE: &'static Shape<'static> = &const {
178
                fn inner_shape() -> &'static Shape<'static> {
×
179
                    <$float as Facet>::SHAPE
×
180
                }
×
181

182
                Shape::builder_for_sized::<Self>()
183
                    .ty(Type::User(UserType::Opaque))
184
                    .def(Def::Scalar(
185
                        ScalarDef::builder()
186
                            .affinity(&const { ScalarAffinity::opaque().build() })
187
                            .build(),
188
                    ))
189
                    .inner(inner_shape)
190
                    .build()
191
            };
192
        }
193
    };
194
}
195

196
impl_facet_for_ordered_float_and_notnan!(f32);
197
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