rune/runtime/
type_info.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
use core::fmt;
use core::hash;

use crate as rune;
use crate::alloc::prelude::*;
use crate::compile::Named;
use crate::hash::Hash;
use crate::{Any, TypeHash};

use ::rust_alloc::sync::Arc;

use super::Rtti;

#[derive(Debug, TryClone, PartialEq, Eq)]
enum TypeInfoKind {
    /// Reference to an external type.
    Any(AnyTypeInfo),
    /// A named type.
    Runtime(Arc<Rtti>),
}

/// Diagnostical type information for a given type.
///
/// Has reasonable [`Debug`] and [`Display`] implementations to identify a given
/// type.
///
/// [`Debug`]: core::fmt::Debug
/// [`Display`]: core::fmt::Display
#[derive(TryClone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TypeInfo {
    kind: TypeInfoKind,
}

impl TypeInfo {
    #[inline]
    const fn new(kind: TypeInfoKind) -> Self {
        Self { kind }
    }

    /// Construct type info for the empty type.
    pub const fn empty() -> Self {
        Self::new(TypeInfoKind::Any(AnyTypeInfo {
            full_name: |f| write!(f, "empty"),
            hash: crate::hash!(::std::empty::Empty),
        }))
    }

    /// Construct type info from an statically known [`Any`] type.
    #[inline]
    pub const fn any<T>() -> Self
    where
        T: Any,
    {
        Self::any_type_info(T::ANY_TYPE_INFO)
    }

    /// Construct type info from an statically known [`Named`] type.
    #[inline]
    pub const fn named<T>() -> Self
    where
        T: Named + TypeHash,
    {
        Self::any_type_info(AnyTypeInfo::new(T::full_name, T::HASH))
    }

    /// Construct type info from an statically known [`Any`] type.
    #[doc(hidden)]
    #[inline]
    pub(crate) const fn any_type_info(type_info: AnyTypeInfo) -> Self {
        Self::new(TypeInfoKind::Any(type_info))
    }

    #[inline]
    pub(crate) const fn rtti(rtti: Arc<Rtti>) -> Self {
        Self::new(TypeInfoKind::Runtime(rtti))
    }

    #[cfg(feature = "emit")]
    pub(crate) fn type_hash(&self) -> Hash {
        match &self.kind {
            TypeInfoKind::Any(ty) => ty.hash,
            TypeInfoKind::Runtime(ty) => ty.type_hash(),
        }
    }
}

impl fmt::Debug for TypeInfo {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl fmt::Display for TypeInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            TypeInfoKind::Runtime(rtti) => {
                write!(f, "{}", rtti.item)?;
            }
            TypeInfoKind::Any(info) => {
                write!(f, "{info}")?;
            }
        }

        Ok(())
    }
}

impl From<AnyTypeInfo> for TypeInfo {
    #[inline]
    fn from(type_info: AnyTypeInfo) -> Self {
        Self::any_type_info(type_info)
    }
}

/// Type information for the [`Any`][crate::Any] type.
#[derive(Debug, TryClone, Clone, Copy)]
#[try_clone(copy)]
pub struct AnyTypeInfo {
    /// Formatter to display a full name.
    pub(crate) full_name: FullNameFn,
    /// The type hash of the item.
    pub(crate) hash: Hash,
}

impl AnyTypeInfo {
    /// Private constructor, use at your own risk.
    pub(crate) const fn new(full_name: FullNameFn, hash: Hash) -> Self {
        Self { full_name, hash }
    }
}

impl fmt::Display for AnyTypeInfo {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        (self.full_name)(f)
    }
}

pub type FullNameFn = fn(&mut fmt::Formatter<'_>) -> fmt::Result;

impl PartialEq for AnyTypeInfo {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

impl Eq for AnyTypeInfo {}

impl hash::Hash for AnyTypeInfo {
    #[inline]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.hash.hash(state);
    }
}