/
std_impls.rs
289 lines (267 loc) · 10.9 KB
/
std_impls.rs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use bones_utils::fxhash::FxHasher;
use crate::{prelude::*, raw_fns::*};
use std::{any::TypeId, hash::Hasher, sync::OnceLock};
macro_rules! impl_primitive {
($t:ty, $prim:ident) => {
unsafe impl HasSchema for $t {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
kind: SchemaKind::Primitive(Primitive::$prim),
type_id: Some(TypeId::of::<$t>()),
clone_fn: Some(<$t as RawClone>::raw_clone),
drop_fn: Some(<$t as RawDrop>::raw_drop),
default_fn: Some(<$t as RawDefault>::raw_default),
hash_fn: Some(<$t as RawHash>::raw_hash),
eq_fn: Some(<$t as RawEq>::raw_eq),
type_data: Default::default(),
})
})
}
}
};
}
impl_primitive!(String, String);
impl_primitive!(bool, Bool);
impl_primitive!(u8, U8);
impl_primitive!(u16, U16);
impl_primitive!(u32, U32);
impl_primitive!(u64, U64);
impl_primitive!(u128, U128);
impl_primitive!(i8, I8);
impl_primitive!(i16, I16);
impl_primitive!(i32, I32);
impl_primitive!(i64, I64);
impl_primitive!(i128, I128);
macro_rules! schema_impl_float {
($t:ty, $prim:ident) => {
unsafe impl HasSchema for $t {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
kind: SchemaKind::Primitive(Primitive::$prim),
type_id: Some(TypeId::of::<$t>()),
clone_fn: Some(<$t as RawClone>::raw_clone),
drop_fn: Some(<$t as RawDrop>::raw_drop),
default_fn: Some(<$t as RawDefault>::raw_default),
hash_fn: Some(<$t as CustomRawFns>::raw_hash),
eq_fn: Some(<$t as CustomRawFns>::raw_eq),
type_data: Default::default(),
})
})
}
}
};
}
schema_impl_float!(f32, F32);
schema_impl_float!(f64, F64);
unsafe impl HasSchema for usize {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
kind: SchemaKind::Primitive({
#[cfg(target_pointer_width = "32")]
let p = Primitive::U32;
#[cfg(target_pointer_width = "64")]
let p = Primitive::U64;
p
}),
type_id: Some(TypeId::of::<usize>()),
clone_fn: Some(<Self as RawClone>::raw_clone),
drop_fn: Some(<Self as RawDrop>::raw_drop),
default_fn: Some(<Self as RawDefault>::raw_default),
hash_fn: Some(<Self as RawHash>::raw_hash),
eq_fn: Some(<Self as RawEq>::raw_eq),
type_data: Default::default(),
})
})
}
}
unsafe impl HasSchema for isize {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
kind: SchemaKind::Primitive({
#[cfg(target_pointer_width = "32")]
let p = Primitive::I32;
#[cfg(target_pointer_width = "64")]
let p = Primitive::I64;
p
}),
type_id: Some(TypeId::of::<Self>()),
clone_fn: Some(<Self as RawClone>::raw_clone),
drop_fn: Some(<Self as RawDrop>::raw_drop),
default_fn: Some(<Self as RawDefault>::raw_default),
hash_fn: Some(<Self as RawHash>::raw_hash),
eq_fn: Some(<Self as RawEq>::raw_eq),
type_data: Default::default(),
})
})
}
}
#[cfg(feature = "glam")]
mod impl_glam {
use super::*;
use glam::*;
unsafe impl HasSchema for Quat {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
let layout = std::alloc::Layout::new::<Quat>();
S.get_or_init(|| {
SCHEMA_REGISTRY.register(SchemaData {
kind: SchemaKind::Primitive(Primitive::Opaque {
size: layout.size(),
align: layout.align(),
}),
type_id: Some(TypeId::of::<usize>()),
clone_fn: Some(<Self as RawClone>::raw_clone),
drop_fn: Some(<Self as RawDrop>::raw_drop),
default_fn: Some(<Self as RawDefault>::raw_default),
// TODO: Implement hash and eq for `Quat`.
hash_fn: None,
eq_fn: None,
type_data: Default::default(),
})
})
}
}
macro_rules! schema_impl_glam {
($t:ty, $prim:ident, $nprim:ident, $($field:ident),+) => {
unsafe impl HasSchema for $t {
fn schema() -> &'static Schema {
static S: OnceLock<&'static Schema> = OnceLock::new();
S.get_or_init(|| {
let type_id = Some(TypeId::of::<Self>());
let kind = SchemaKind::Struct(StructSchemaInfo {
fields: vec![
$(
StructFieldInfo {
name: Some(stringify!($field).into()),
schema: $nprim::schema(),
}
),*
],
});
SCHEMA_REGISTRY.register(SchemaData {
type_id,
kind,
type_data: Default::default(),
clone_fn: Some(<Self as RawClone>::raw_clone),
drop_fn: Some(<Self as RawDrop>::raw_drop),
default_fn: Some(<Self as RawDefault>::raw_default),
hash_fn: Some(<Self as CustomRawFns>::raw_hash),
eq_fn: Some(<Self as CustomRawFns>::raw_eq),
})
})
}
}
};
}
macro_rules! schema_impl_glam_vecs {
($prim:ident, $nprim:ident, $id:ident) => {
paste::paste! {
schema_impl_glam!( [< $id 2 >], $prim, $nprim, x, y);
schema_impl_glam!( [< $id 3 >], $prim, $nprim, x, y, z);
schema_impl_glam!( [< $id 4 >], $prim, $nprim, x, y, z, w);
}
};
}
schema_impl_glam_vecs!(Bool, bool, BVec);
schema_impl_glam_vecs!(U32, u32, UVec);
schema_impl_glam_vecs!(I32, i32, IVec);
schema_impl_glam_vecs!(F32, f32, Vec);
schema_impl_glam_vecs!(F64, f64, DVec);
// TODO: Implement `HasSchema` for glam matrix types.
macro_rules! custom_fns_impl_bvec {
($ty:ident) => {
impl CustomRawFns for glam::$ty {
unsafe extern "C-unwind" fn raw_hash(ptr: *const u8) -> u64 {
<Self as RawHash>::raw_hash(ptr)
}
unsafe extern "C-unwind" fn raw_eq(a: *const u8, b: *const u8) -> bool {
<Self as RawEq>::raw_eq(a, b)
}
}
};
}
custom_fns_impl_bvec!(BVec2);
custom_fns_impl_bvec!(BVec3);
custom_fns_impl_bvec!(BVec4);
macro_rules! custom_fns_impl_glam {
($t:ty, $prim:ident, $($field:ident),+) => {
impl CustomRawFns for $t {
unsafe extern "C-unwind" fn raw_hash(ptr: *const u8) -> u64 {
let this = unsafe { &*(ptr as *const Self) };
let mut hasher = FxHasher::default();
$(
hasher.write_u64($prim::raw_hash(&this.$field as *const $prim as *const u8));
)+
hasher.finish()
}
unsafe extern "C-unwind" fn raw_eq(a: *const u8, b: *const u8) -> bool {
let a = unsafe { &*(a as *const Self) };
let b = unsafe { &*(b as *const Self) };
$(
$prim::raw_eq(
&a.$field as *const $prim as *const u8,
&b.$field as *const $prim as *const u8,
)
)&&+
}
}
};
}
custom_fns_impl_glam!(Vec2, f32, x, y);
custom_fns_impl_glam!(Vec3, f32, x, y, z);
custom_fns_impl_glam!(Vec4, f32, x, y, z, w);
custom_fns_impl_glam!(DVec2, f64, x, y);
custom_fns_impl_glam!(DVec3, f64, x, y, z);
custom_fns_impl_glam!(DVec4, f64, x, y, z, w);
custom_fns_impl_glam!(UVec2, u32, x, y);
custom_fns_impl_glam!(UVec3, u32, x, y, z);
custom_fns_impl_glam!(UVec4, u32, x, y, z, w);
custom_fns_impl_glam!(IVec2, i32, x, y);
custom_fns_impl_glam!(IVec3, i32, x, y, z);
custom_fns_impl_glam!(IVec4, i32, x, y, z, w);
custom_fns_impl_glam!(Quat, f32, x, y, z, w);
}
/// Trait for types that require specific implementations of eq and hash fns, for use in this module only.
trait CustomRawFns {
unsafe extern "C-unwind" fn raw_hash(ptr: *const u8) -> u64;
unsafe extern "C-unwind" fn raw_eq(a: *const u8, b: *const u8) -> bool;
}
macro_rules! custom_fns_impl_float {
($ty:ident) => {
impl CustomRawFns for $ty {
unsafe extern "C-unwind" fn raw_hash(ptr: *const u8) -> u64 {
let this = unsafe { &*(ptr as *const Self) };
let mut hasher = FxHasher::default();
if this.is_nan() {
// Ensure all NaN representations hash to the same value
hasher.write(&$ty::to_ne_bytes($ty::NAN));
} else if *this == 0.0 {
// Ensure both zeroes hash to the same value
hasher.write(&$ty::to_ne_bytes(0.0));
} else {
hasher.write(&$ty::to_ne_bytes(*this));
}
hasher.finish()
}
unsafe extern "C-unwind" fn raw_eq(a: *const u8, b: *const u8) -> bool {
let a = unsafe { &*(a as *const Self) };
let b = unsafe { &*(b as *const Self) };
if a.is_nan() && a.is_nan() {
true
} else {
a == b
}
}
}
};
}
custom_fns_impl_float!(f32);
custom_fns_impl_float!(f64);