Skip to main content

facet_core/impls/core/
infallible.rs

1use crate::{
2    Def, Facet, HashProxy, OxPtrConst, OxPtrMut, Repr, Shape, ShapeBuilder, StructKind, StructType,
3    Type, TypeOpsIndirect, UserType, VTableIndirect,
4};
5
6const unsafe fn infallible_drop(_ptr: OxPtrMut) {
7    // Infallible is zero-sized, nothing to drop
8}
9
10// Infallible vtable - implementations that can never be called since Infallible cannot be instantiated
11const INFALLIBLE_VTABLE: VTableIndirect = VTableIndirect {
12    display: None,
13    debug: Some(infallible_debug),
14    hash: Some(infallible_hash),
15    invariants: None,
16    parse: None,
17    parse_bytes: None,
18    try_from: None,
19    try_into_inner: None,
20    try_borrow_inner: None,
21    partial_eq: Some(infallible_partial_eq),
22    partial_cmp: Some(infallible_partial_cmp),
23    cmp: Some(infallible_cmp),
24};
25
26// Type operations for Infallible
27static INFALLIBLE_TYPE_OPS: TypeOpsIndirect = TypeOpsIndirect {
28    drop_in_place: infallible_drop,
29    default_in_place: None, // Infallible cannot be constructed
30    clone_into: None,
31    is_truthy: None,
32};
33
34unsafe fn infallible_debug(
35    _ox: OxPtrConst,
36    f: &mut core::fmt::Formatter<'_>,
37) -> Option<core::fmt::Result> {
38    // This can never be called since Infallible cannot be instantiated
39    Some(f.write_str("Infallible"))
40}
41
42const unsafe fn infallible_hash(_ox: OxPtrConst, _hasher: &mut HashProxy<'_>) -> Option<()> {
43    // This can never be called since Infallible cannot be instantiated
44    Some(())
45}
46
47const unsafe fn infallible_partial_eq(_a: OxPtrConst, _b: OxPtrConst) -> Option<bool> {
48    // This can never be called since Infallible cannot be instantiated
49    Some(true)
50}
51
52const unsafe fn infallible_partial_cmp(
53    _a: OxPtrConst,
54    _b: OxPtrConst,
55) -> Option<Option<core::cmp::Ordering>> {
56    // This can never be called since Infallible cannot be instantiated
57    Some(Some(core::cmp::Ordering::Equal))
58}
59
60const unsafe fn infallible_cmp(_a: OxPtrConst, _b: OxPtrConst) -> Option<core::cmp::Ordering> {
61    // This can never be called since Infallible cannot be instantiated
62    Some(core::cmp::Ordering::Equal)
63}
64
65unsafe impl Facet<'_> for core::convert::Infallible {
66    const SHAPE: &'static Shape = &const {
67        // Infallible implements Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash
68        // but NOT Default (cannot be constructed) or Display
69
70        ShapeBuilder::for_sized::<core::convert::Infallible>("Infallible")
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(&INFALLIBLE_VTABLE)
78            .type_ops_indirect(&INFALLIBLE_TYPE_OPS)
79            .eq()
80            .copy()
81            .send()
82            .sync()
83            .build()
84    };
85}
86
87// Note: The never type (!) implementation is currently not included because
88// it requires the unstable `never_type` feature. Once the feature is stabilized,
89// we can add the implementation here following the same pattern as Infallible.