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

facet-rs / facet / 15221507418

24 May 2025 12:29AM UTC coverage: 57.643% (+0.1%) from 57.538%
15221507418

push

github

kylewlacy
Update `hacking.md`

9880 of 17140 relevant lines covered (57.64%)

137.72 hits per line

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

14.56
/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>() };
1✔
21
                        let ord = OrderedFloat(*value);
1✔
22
                        Ok(unsafe { dst.put(ord) })
1✔
23
                    } else {
24
                        let inner_try_from = (<$float as Facet>::SHAPE.vtable.try_from)().ok_or(
×
25
                            TryFromError::UnsupportedSourceShape {
×
26
                                src_shape,
×
27
                                expected: &[<$float as Facet>::SHAPE],
×
28
                            },
×
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) };
×
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
                }
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"));
3✔
64
                vtable.parse = || {
×
65
                    // `OrderedFloat` is `repr(transparent)`
66
                    (<$float as Facet>::SHAPE.vtable.parse)()
×
67
                };
×
68
                vtable.try_from = || Some(try_from);
1✔
69
                vtable.try_into_inner = || Some(try_into_inner);
×
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> {
3✔
76
                    <$float as Facet>::SHAPE
3✔
77
                }
3✔
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 {
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
158
                vtable.parse = || {
×
159
                    Some(|s, target| match s.parse::<$float>() {
×
160
                        Ok(inner) => match NotNan::new(inner) {
×
161
                            Ok(not_nan) => Ok(unsafe { target.put(not_nan) }),
×
162
                            Err(_) => {
163
                                Err(crate::ParseError::Generic("NaN is not allowed for NotNan"))
×
164
                            }
165
                        },
166
                        Err(_) => Err(crate::ParseError::Generic(
×
167
                            "Failed to parse inner type for NotNan",
×
168
                        )),
×
169
                    })
×
170
                };
×
171
                vtable.try_from = || Some(try_from);
×
172
                vtable.try_into_inner = || Some(try_into_inner);
×
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