rune/runtime/
type.rs

1#[cfg(feature = "musli")]
2use musli::{Decode, Encode};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5
6use crate as rune;
7use crate::compile::Named;
8use crate::module::InstallWith;
9use crate::runtime::RuntimeError;
10use crate::{item, FromValue, Hash, Item, Value};
11
12/// A value representing a type in the virtual machine.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[repr(transparent)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
16#[cfg_attr(feature = "musli", derive(Decode, Encode), musli(transparent))]
17pub struct Type(Hash);
18
19impl Type {
20    /// Construct a new type.
21    pub(crate) fn new(hash: Hash) -> Self {
22        Self(hash)
23    }
24
25    /// Coerce into inner type hash.
26    #[inline]
27    pub fn into_hash(self) -> Hash {
28        self.0
29    }
30}
31
32impl InstallWith for Type {}
33
34impl FromValue for Type {
35    #[inline]
36    fn from_value(value: Value) -> Result<Self, RuntimeError> {
37        value.as_type()
38    }
39}
40
41impl Named for Type {
42    const ITEM: &'static Item = item!(::std::any::Type);
43}