1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![cfg(feature = "ordered-float")]
use crate::{
Def, Facet, FieldBuilder, Repr, Shape, ShapeBuilder, StructKind, StructType, TryFromOutcome,
Type, TypeOpsDirect, UserType, VTableDirect, type_ops_direct, vtable_direct,
};
use ordered_float::{NotNan, OrderedFloat};
macro_rules! impl_facet_for_ordered_float_and_notnan {
($float:ty) => {
unsafe impl<'a> Facet<'a> for OrderedFloat<$float> {
const SHAPE: &'static Shape = &const {
// OrderedFloat implements Display, Debug, Hash, PartialEq, Eq, PartialOrd, Ord
// It also implements Clone, Copy, Default, FromStr
const VTABLE: VTableDirect = vtable_direct!(OrderedFloat<$float> =>
FromStr,
Display,
Debug,
Hash,
PartialEq,
PartialOrd,
Ord,
);
const TYPE_OPS: TypeOpsDirect = type_ops_direct!(OrderedFloat<$float> => Default, Clone);
ShapeBuilder::for_sized::<OrderedFloat<$float>>("OrderedFloat")
.module_path("ordered_float")
.ty(Type::User(UserType::Struct(StructType {
repr: Repr::transparent(),
kind: StructKind::Tuple,
fields: &const { [FieldBuilder::new("0", crate::shape_of::<$float>, 0).build()] },
})))
.def(Def::Scalar)
.inner(<$float as Facet>::SHAPE)
.vtable_direct(&VTABLE)
.type_ops_direct(&TYPE_OPS)
.eq()
.copy()
.send()
.sync()
.build()
};
}
unsafe impl<'a> Facet<'a> for NotNan<$float> {
const SHAPE: &'static Shape = &const {
// Custom parse function that enforces NotNan invariant
unsafe fn parse_notnan(
s: &str,
target: *mut NotNan<$float>,
) -> Result<(), crate::ParseError> {
match s.parse::<$float>() {
Ok(inner) => match NotNan::new(inner) {
Ok(not_nan) => {
unsafe { target.write(not_nan) };
Ok(())
}
Err(_) => Err(crate::ParseError::Str("NaN is not allowed for NotNan")),
},
Err(_) => Err(crate::ParseError::Str(
"Failed to parse inner type for NotNan",
)),
}
}
// try_from implementation to accept inner float type
unsafe fn try_from_notnan(
target: *mut NotNan<$float>,
src_shape: &'static Shape,
src: crate::PtrConst,
) -> TryFromOutcome {
// Accept the inner float type (floats are Copy, so we use get)
if src_shape.id == <$float as Facet>::SHAPE.id {
let float_val = unsafe { *src.get::<$float>() };
match NotNan::new(float_val) {
Ok(not_nan) => {
unsafe { target.write(not_nan) };
TryFromOutcome::Converted
}
Err(_) => TryFromOutcome::Failed("NaN is not allowed for NotNan".into()),
}
} else if src_shape.id == <f64 as Facet>::SHAPE.id {
// Also accept f64 and convert (for ScalarValue::F64)
let float_val = unsafe { *src.get::<f64>() } as $float;
match NotNan::new(float_val) {
Ok(not_nan) => {
unsafe { target.write(not_nan) };
TryFromOutcome::Converted
}
Err(_) => TryFromOutcome::Failed("NaN is not allowed for NotNan".into()),
}
} else {
TryFromOutcome::Unsupported
}
}
// NotNan implements Display, Debug, Hash, PartialEq, Eq, PartialOrd, Ord
// It also implements Clone, Copy, FromStr (but we override parse)
// It does NOT implement Default (no default value for NotNan)
const VTABLE: VTableDirect = vtable_direct!(NotNan<$float> =>
Display,
Debug,
Hash,
PartialEq,
PartialOrd,
Ord,
[parse = parse_notnan],
[try_from = try_from_notnan],
);
const TYPE_OPS: TypeOpsDirect = type_ops_direct!(NotNan<$float> => Clone);
ShapeBuilder::for_sized::<NotNan<$float>>("NotNan")
.module_path("ordered_float")
.ty(Type::User(UserType::Opaque))
.def(Def::Scalar)
.inner(<$float as Facet>::SHAPE)
.vtable_direct(&VTABLE)
.type_ops_direct(&TYPE_OPS)
.eq()
.copy()
.send()
.sync()
.build()
};
}
};
}
impl_facet_for_ordered_float_and_notnan!(f32);
impl_facet_for_ordered_float_and_notnan!(f64);