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

facet-rs / facet / 19825640883

01 Dec 2025 02:13PM UTC coverage: 54.918% (-3.1%) from 58.04%
19825640883

Pull #978

github

web-flow
Merge b9eeedede into e3f8681ec
Pull Request #978: Dodeca: Font subsetting, cache busting, and parser-based URL rewriting

2525 of 6545 new or added lines in 23 files covered. (38.58%)

43 existing lines in 11 files now uncovered.

21373 of 38918 relevant lines covered (54.92%)

152.19 hits per line

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

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

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

58
                        // Borrow inner float type
59
                        unsafe fn try_borrow_inner(
×
60
                            src_ptr: PtrConst<'_>,
×
61
                        ) -> Result<PtrConst<'_>, TryBorrowInnerError> {
×
62
                            let v = unsafe { src_ptr.get::<OrderedFloat<$float>>() };
×
63
                            Ok(PtrConst::new((&v.0).into()))
×
64
                        }
×
65

UNCOV
66
                        let mut vtable = value_vtable!((), |f, _opts| write!(
×
67
                            f,
×
68
                            "{}",
69
                            Self::SHAPE.type_identifier
×
70
                        ));
71
                        {
72
                            vtable.parse = {
73
                                // `OrderedFloat` is `repr(transparent)`
74
                                <$float as Facet>::SHAPE.vtable.parse
75
                            };
76
                            vtable.try_from = Some(try_from);
77
                            vtable.try_into_inner = Some(try_into_inner);
78
                            vtable.try_borrow_inner = Some(try_borrow_inner);
79
                        }
80
                        vtable
81
                    })
82
                    .type_identifier("OrderedFloat")
83
                    .ty(Type::User(UserType::Struct(
84
                        StructType::builder()
85
                            .repr(Repr::transparent())
86
                            .fields(&const { [field_in_type!(Self, 0)] })
87
                            .kind(crate::StructKind::Tuple)
88
                            .build(),
89
                    )))
90
                    .def(Def::Scalar)
91
                    .inner(<$float as Facet>::SHAPE)
92
                    .build()
93
            };
94
        }
95

96
        unsafe impl<'a> Facet<'a> for NotNan<$float> {
97
            const SHAPE: &'static Shape = &const {
98
                Shape::builder_for_sized::<Self>()
99
                    .vtable({
100
                        // Conversion from inner float type to NotNan<$float>
101
                        unsafe fn try_from<'dst>(
×
102
                            src_ptr: PtrConst<'_>,
×
103
                            src_shape: &'static Shape,
×
104
                            dst: PtrUninit<'dst>,
×
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 = NotNan::new(value)
×
110
                                    .map_err(|_| TryFromError::Generic("was NaN"))?;
×
111
                                Ok(unsafe { dst.put(nn) })
×
112
                            } else {
113
                                let inner_try_from = <$float as Facet>::SHAPE
×
114
                                    .vtable
×
115
                                    .try_from
×
116
                                    .ok_or(TryFromError::UnsupportedSourceShape {
×
117
                                        src_shape,
×
118
                                        expected: &[<$float as Facet>::SHAPE],
×
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 =
×
125
                                    unsafe { (inner_try_from)(src_ptr, src_shape, dst) };
×
126
                                match inner_result {
×
127
                                    Ok(result) => {
×
128
                                        // After conversion to inner type, wrap as NotNan
129
                                        let value = unsafe { *result.get::<$float>() };
×
130
                                        let nn = NotNan::new(value)
×
131
                                            .map_err(|_| TryFromError::Generic("was NaN"))?;
×
132
                                        Ok(unsafe { dst.put(nn) })
×
133
                                    }
134
                                    Err(e) => Err(e),
×
135
                                }
136
                            }
137
                        }
×
138

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

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

UNCOV
156
                        let mut vtable = value_vtable!((), |f, _opts| write!(
×
157
                            f,
×
158
                            "{}",
159
                            Self::SHAPE.type_identifier
×
160
                        ));
161
                        // Accept parsing as inner T, but enforce NotNan invariant
162
                        {
163
                            vtable.parse = {
164
                                Some(|s, target| match s.parse::<$float>() {
×
165
                                    Ok(inner) => match NotNan::new(inner) {
×
166
                                        Ok(not_nan) => Ok(unsafe { target.put(not_nan) }),
×
167
                                        Err(_) => Err(crate::ParseError::Generic(
×
168
                                            "NaN is not allowed for NotNan",
×
169
                                        )),
×
170
                                    },
171
                                    Err(_) => Err(crate::ParseError::Generic(
×
172
                                        "Failed to parse inner type for NotNan",
×
173
                                    )),
×
174
                                })
×
175
                            };
176
                            vtable.try_from = Some(try_from);
177
                            vtable.try_into_inner = Some(try_into_inner);
178
                            vtable.try_borrow_inner = Some(try_borrow_inner);
179
                        }
180
                        vtable
181
                    })
182
                    .type_identifier("NotNan")
183
                    .ty(Type::User(UserType::Opaque))
184
                    .def(Def::Scalar)
185
                    .inner(<$float as Facet>::SHAPE)
186
                    .build()
187
            };
188
        }
189
    };
190
}
191

192
impl_facet_for_ordered_float_and_notnan!(f32);
193
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

© 2026 Coveralls, Inc