facet-core 0.46.0

Core reflection traits and types for the facet ecosystem - provides the Facet trait, Shape metadata, and type-erased pointers
Documentation
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Tests for the new VTable API

use facet_core::{Facet, OxRef, VTableErased};

#[test]
fn test_bool_debug() {
    let shape = <bool as Facet>::SHAPE;
    let vtable = shape.vtable;

    // VTableErased should be Direct for bool
    match vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.debug.is_some(), "bool should have debug");
            assert!(vt.display.is_some(), "bool should have display");
            assert!(vt.partial_eq.is_some(), "bool should have partial_eq");
            assert!(vt.cmp.is_some(), "bool should have cmp");
        }
        VTableErased::Indirect(_) => {
            panic!("bool should use Direct vtable, not Indirect");
        }
    }
}

#[test]
fn test_str_indirect() {
    let shape = <str as Facet>::SHAPE;
    let vtable = shape.vtable;

    // VTableErased should be Indirect for str (unsized)
    match vtable {
        VTableErased::Direct(_) => {
            panic!("str should use Indirect vtable, not Direct");
        }
        VTableErased::Indirect(vt) => {
            assert!(vt.debug.is_some(), "str should have debug");
            assert!(vt.display.is_some(), "str should have display");
            assert!(vt.partial_eq.is_some(), "str should have partial_eq");
            assert!(vt.cmp.is_some(), "str should have cmp");
        }
    }
}

#[test]
fn test_integers_have_vtable() {
    // All integers should have Direct vtables with standard traits
    macro_rules! check_integer {
        ($t:ty) => {
            let shape = <$t as Facet>::SHAPE;
            match shape.vtable {
                VTableErased::Direct(vt) => {
                    assert!(
                        vt.debug.is_some(),
                        concat!(stringify!($t), " should have debug")
                    );
                    assert!(
                        vt.display.is_some(),
                        concat!(stringify!($t), " should have display")
                    );
                    assert!(
                        vt.partial_eq.is_some(),
                        concat!(stringify!($t), " should have partial_eq")
                    );
                    assert!(
                        vt.cmp.is_some(),
                        concat!(stringify!($t), " should have cmp")
                    );
                }
                VTableErased::Indirect(_) => {
                    panic!(concat!(stringify!($t), " should use Direct vtable"));
                }
            }
        };
    }

    check_integer!(u8);
    check_integer!(i8);
    check_integer!(u16);
    check_integer!(i16);
    check_integer!(u32);
    check_integer!(i32);
    check_integer!(u64);
    check_integer!(i64);
    check_integer!(u128);
    check_integer!(i128);
    check_integer!(usize);
    check_integer!(isize);
}

#[test]
fn test_hash_present() {
    // Types that implement Hash should have hash in vtable
    let bool_shape = <bool as Facet>::SHAPE;
    match bool_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.hash.is_some(), "bool should have hash");
        }
        _ => panic!("bool should use Direct vtable"),
    }

    let u32_shape = <u32 as Facet>::SHAPE;
    match u32_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.hash.is_some(), "u32 should have hash");
        }
        _ => panic!("u32 should use Direct vtable"),
    }

    let char_shape = <char as Facet>::SHAPE;
    match char_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.hash.is_some(), "char should have hash");
        }
        _ => panic!("char should use Direct vtable"),
    }

    let str_shape = <str as Facet>::SHAPE;
    match str_shape.vtable {
        VTableErased::Indirect(vt) => {
            assert!(vt.hash.is_some(), "str should have hash");
        }
        _ => panic!("str should use Indirect vtable"),
    }
}

#[test]
fn test_floats_no_hash() {
    // Floats don't implement Hash (because of NaN)
    let f32_shape = <f32 as Facet>::SHAPE;
    match f32_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.hash.is_none(), "f32 should NOT have hash (NaN)");
        }
        _ => panic!("f32 should use Direct vtable"),
    }

    let f64_shape = <f64 as Facet>::SHAPE;
    match f64_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.hash.is_none(), "f64 should NOT have hash (NaN)");
        }
        _ => panic!("f64 should use Direct vtable"),
    }
}

#[test]
fn test_floats_no_ord() {
    // Floats should NOT have cmp (because of NaN)
    let f32_shape = <f32 as Facet>::SHAPE;
    match f32_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.debug.is_some(), "f32 should have debug");
            assert!(vt.partial_eq.is_some(), "f32 should have partial_eq");
            assert!(vt.partial_cmp.is_some(), "f32 should have partial_cmp");
            assert!(vt.cmp.is_none(), "f32 should NOT have cmp (NaN)");
        }
        _ => panic!("f32 should use Direct vtable"),
    }

    let f64_shape = <f64 as Facet>::SHAPE;
    match f64_shape.vtable {
        VTableErased::Direct(vt) => {
            assert!(vt.debug.is_some(), "f64 should have debug");
            assert!(vt.partial_eq.is_some(), "f64 should have partial_eq");
            assert!(vt.partial_cmp.is_some(), "f64 should have partial_cmp");
            assert!(vt.cmp.is_none(), "f64 should NOT have cmp (NaN)");
        }
        _ => panic!("f64 should use Direct vtable"),
    }
}

#[test]
fn test_option_vtable_indirect() {
    // Option<T> uses Indirect vtable
    let shape = <Option<i32> as Facet>::SHAPE;
    match shape.vtable {
        VTableErased::Direct(_) => {
            panic!("Option<i32> should use Indirect vtable, not Direct");
        }
        VTableErased::Indirect(vt) => {
            assert!(vt.debug.is_some(), "Option should have debug");
            assert!(vt.hash.is_some(), "Option should have hash");
            assert!(vt.partial_eq.is_some(), "Option should have partial_eq");
            assert!(vt.partial_cmp.is_some(), "Option should have partial_cmp");
            assert!(vt.cmp.is_some(), "Option should have cmp");
        }
    }
    // drop_in_place moved from VTable to TypeOps
    assert!(shape.type_ops.is_some(), "Option should have type_ops");
}

#[test]
fn test_option_debug_some() {
    let value: Option<i32> = Some(42);
    let ox = OxRef::from_ref(&value);

    let debug_str = format!("{ox:?}");
    assert_eq!(debug_str, "Some(42)");
}

#[test]
fn test_option_debug_none() {
    let value: Option<i32> = None;
    let ox = OxRef::from_ref(&value);

    let debug_str = format!("{ox:?}");
    assert_eq!(debug_str, "None");
}

#[test]
fn test_option_partial_eq() {
    let some1: Option<i32> = Some(42);
    let some2: Option<i32> = Some(42);
    let some3: Option<i32> = Some(99);
    let none: Option<i32> = None;

    let ox1 = OxRef::from_ref(&some1);
    let ox2 = OxRef::from_ref(&some2);
    let ox3 = OxRef::from_ref(&some3);
    let ox_none = OxRef::from_ref(&none);

    // Some(42) == Some(42)
    assert!(ox1 == ox2);
    // Some(42) != Some(99)
    assert!(ox1 != ox3);
    // Some(42) != None
    assert!(ox1 != ox_none);
    // None == None
    assert!(ox_none == ox_none);
}

#[test]
fn test_option_partial_cmp() {
    let some1: Option<i32> = Some(42);
    let some2: Option<i32> = Some(99);
    let none: Option<i32> = None;

    let ox1 = OxRef::from_ref(&some1);
    let ox2 = OxRef::from_ref(&some2);
    let ox_none = OxRef::from_ref(&none);

    use std::cmp::Ordering;

    // None < Some(_)
    assert_eq!(ox_none.partial_cmp(&ox1), Some(Ordering::Less));
    // Some(42) < Some(99)
    assert_eq!(ox1.partial_cmp(&ox2), Some(Ordering::Less));
    // Some(99) > Some(42)
    assert_eq!(ox2.partial_cmp(&ox1), Some(Ordering::Greater));
    // None == None
    assert_eq!(ox_none.partial_cmp(&ox_none), Some(Ordering::Equal));
}

#[test]
fn test_option_hash() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let some1: Option<i32> = Some(42);
    let some2: Option<i32> = Some(42);
    let none: Option<i32> = None;

    let ox1 = OxRef::from_ref(&some1);
    let ox2 = OxRef::from_ref(&some2);
    let ox_none = OxRef::from_ref(&none);

    let mut h1 = DefaultHasher::new();
    let mut h2 = DefaultHasher::new();
    let mut h_none = DefaultHasher::new();

    ox1.hash(&mut h1);
    ox2.hash(&mut h2);
    ox_none.hash(&mut h_none);

    // Same values should have same hash
    assert_eq!(h1.finish(), h2.finish());
    // Different values should (likely) have different hashes
    // (not guaranteed, but highly likely for these values)
    assert_ne!(h1.finish(), h_none.finish());
}

#[test]
fn test_option_nested_debug() {
    // Test nested Option
    let value: Option<Option<i32>> = Some(Some(42));
    let ox = OxRef::from_ref(&value);

    let debug_str = format!("{ox:?}");
    assert_eq!(debug_str, "Some(Some(42))");

    let none_inner: Option<Option<i32>> = Some(None);
    let ox_none_inner = OxRef::from_ref(&none_inner);
    assert_eq!(format!("{ox_none_inner:?}"), "Some(None)");

    let none_outer: Option<Option<i32>> = None;
    let ox_none_outer = OxRef::from_ref(&none_outer);
    assert_eq!(format!("{ox_none_outer:?}"), "None");
}

/// Test that Shape hash is consistent with equality.
/// This is a regression test for https://github.com/facet-rs/facet/issues/1574
#[test]
fn test_shape_hash_consistent_with_eq() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    // Get Shape from multiple places - in the same crate they're definitely
    // the same pointer, but the hash/eq must be consistent for HashSet to work.
    let shape1 = <u8 as Facet>::SHAPE;
    let shape2 = <u8 as Facet>::SHAPE;

    // They should be equal
    assert_eq!(shape1, shape2, "Same type's shapes should be equal");

    // If a == b, then hash(a) == hash(b) (Hash trait requirement)
    let mut h1 = DefaultHasher::new();
    let mut h2 = DefaultHasher::new();
    shape1.hash(&mut h1);
    shape2.hash(&mut h2);
    assert_eq!(
        h1.finish(),
        h2.finish(),
        "Equal shapes must have equal hashes"
    );
}

/// Test that ConstTypeId hash is consistent with equality.
#[test]
fn test_const_type_id_hash_consistent_with_eq() {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    use facet_core::ConstTypeId;

    let id1 = ConstTypeId::of::<u8>();
    let id2 = ConstTypeId::of::<u8>();

    // They should be equal
    assert_eq!(id1, id2, "Same type's ConstTypeIds should be equal");

    // If a == b, then hash(a) == hash(b) (Hash trait requirement)
    let mut h1 = DefaultHasher::new();
    let mut h2 = DefaultHasher::new();
    id1.hash(&mut h1);
    id2.hash(&mut h2);
    assert_eq!(
        h1.finish(),
        h2.finish(),
        "Equal ConstTypeIds must have equal hashes"
    );
}

/// Test that Infallible implements Facet correctly.
/// Infallible is a zero-sized type that cannot be instantiated,
/// but should still have Facet implementation for use in Result<T, Infallible>.
#[test]
fn test_infallible_has_facet() {
    use std::convert::Infallible;

    let shape = <Infallible as Facet>::SHAPE;

    // Infallible should use Indirect vtable (like other zero-sized types)
    match shape.vtable {
        VTableErased::Indirect(vt) => {
            assert!(vt.debug.is_some(), "Infallible should have debug");
            assert!(vt.display.is_none(), "Infallible should NOT have display");
            assert!(vt.partial_eq.is_some(), "Infallible should have partial_eq");
            assert!(
                vt.partial_cmp.is_some(),
                "Infallible should have partial_cmp"
            );
            assert!(vt.cmp.is_some(), "Infallible should have cmp");
            assert!(vt.hash.is_some(), "Infallible should have hash");
        }
        VTableErased::Direct(_) => {
            panic!("Infallible should use Indirect vtable, not Direct");
        }
    }

    // Infallible should have type_ops with drop
    assert!(shape.type_ops.is_some(), "Infallible should have type_ops");

    // Verify the type identifier
    assert_eq!(
        shape.type_identifier, "Infallible",
        "Type identifier should be 'Infallible'"
    );
}

/// Test that Result<T, Infallible> can use Facet reflection.
/// This is the primary use case for Infallible implementing Facet.
#[test]
fn test_result_with_infallible() {
    use std::convert::Infallible;

    // This is a common pattern for infallible operations
    let _shape = <Result<i32, Infallible> as Facet>::SHAPE;

    // Just verify it compiles and we can get the shape
    // The actual Ok value can be reflected
    let value: Result<i32, Infallible> = Ok(42);
    let ox = OxRef::from_ref(&value);
    let debug_str = format!("{ox:?}");
    assert_eq!(debug_str, "Ok(42)");
}