facet_core/impls/core/
phantom.rs

1use crate::{
2    Def, Facet, HashProxy, OxPtrConst, OxPtrMut, Repr, Shape, ShapeBuilder, StructKind, StructType,
3    Type, TypeOpsIndirect, UserType, VTableIndirect,
4};
5
6unsafe fn phantom_drop(_ptr: OxPtrMut) {
7    // PhantomData is zero-sized, nothing to drop
8}
9
10unsafe fn phantom_default(_dst: OxPtrMut) {
11    // PhantomData is zero-sized, nothing to write
12}
13
14// Shared vtable for all PhantomData<T> - the implementations don't depend on T
15const PHANTOM_VTABLE: VTableIndirect = VTableIndirect {
16    display: None,
17    debug: Some(phantom_debug),
18    hash: Some(phantom_hash),
19    invariants: None,
20    parse: None,
21    try_from: None,
22    try_into_inner: None,
23    try_borrow_inner: None,
24    partial_eq: Some(phantom_partial_eq),
25    partial_cmp: Some(phantom_partial_cmp),
26    cmp: Some(phantom_cmp),
27};
28
29// Type operations for all PhantomData<T>
30static PHANTOM_TYPE_OPS: TypeOpsIndirect = TypeOpsIndirect {
31    drop_in_place: phantom_drop,
32    default_in_place: Some(phantom_default),
33    clone_into: None,
34    is_truthy: None,
35};
36
37unsafe fn phantom_debug(
38    _ox: OxPtrConst,
39    f: &mut core::fmt::Formatter<'_>,
40) -> Option<core::fmt::Result> {
41    Some(f.write_str("PhantomData"))
42}
43
44unsafe fn phantom_hash(_ox: OxPtrConst, _hasher: &mut HashProxy<'_>) -> Option<()> {
45    // PhantomData hashes to nothing
46    Some(())
47}
48
49unsafe fn phantom_partial_eq(_a: OxPtrConst, _b: OxPtrConst) -> Option<bool> {
50    // All PhantomData are equal
51    Some(true)
52}
53
54unsafe fn phantom_partial_cmp(
55    _a: OxPtrConst,
56    _b: OxPtrConst,
57) -> Option<Option<core::cmp::Ordering>> {
58    Some(Some(core::cmp::Ordering::Equal))
59}
60
61unsafe fn phantom_cmp(_a: OxPtrConst, _b: OxPtrConst) -> Option<core::cmp::Ordering> {
62    Some(core::cmp::Ordering::Equal)
63}
64
65unsafe impl<'a, T: ?Sized + 'a> Facet<'a> for core::marker::PhantomData<T> {
66    const SHAPE: &'static Shape = &const {
67        // PhantomData<T> implements Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash
68        // unconditionally (not depending on T) - but NOT Display
69
70        ShapeBuilder::for_sized::<core::marker::PhantomData<T>>("PhantomData")
71            .ty(Type::User(UserType::Struct(StructType {
72                repr: Repr::default(),
73                kind: StructKind::Unit,
74                fields: &[],
75            })))
76            .def(Def::Scalar)
77            .vtable_indirect(&PHANTOM_VTABLE)
78            .type_ops_indirect(&PHANTOM_TYPE_OPS)
79            .eq()
80            .copy()
81            .send()
82            .sync()
83            .build()
84    };
85}