@@ -56,26 +56,62 @@ use crate::hash::Hash;
56
56
/// ```
57
57
pub use rune_macros:: Any ;
58
58
59
- /// A trait which can be stored inside of an [AnyObj](crate::runtime::AnyObj) .
59
+ /// Derive for types which can be used inside of Rune .
60
60
///
61
- /// We use our own marker trait that must be explicitly derived to prevent other
62
- /// VM native types (like strings) which also implement `std::any:: Any` from
63
- /// being stored as an `AnyObj` .
61
+ /// Rune only supports two types, *built-in* types [`String`] and *external*
62
+ /// types which derive ` Any`. Before they can be used they must be registered in
63
+ /// [`Context::install`] through a [`Module`] .
64
64
///
65
- /// This means, that only types which derive `Any` can be used inside of the VM:
65
+ /// This is typically used in combination with declarative macros to register
66
+ /// functions and macros, such as:
67
+ ///
68
+ /// * [`#[rune::function]`]
69
+ /// * [`#[rune::macro_]`]
70
+ ///
71
+ /// [`AnyObj`]: crate::runtime::AnyObj
72
+ /// [`Context::install`]: crate::Context::install
73
+ /// [`Module`]: crate::Module
74
+ /// [`String`]: std::string::String
75
+ /// [`#[rune::function]`]: crate::function
76
+ /// [`#[rune::macro_]`]: crate::macro_
77
+ ///
78
+ /// # Examples
66
79
///
67
80
/// ```
68
81
/// use rune::Any;
69
82
///
70
83
/// #[derive(Any)]
71
84
/// struct Npc {
72
- /// name: String,
85
+ /// #[rune(get)]
86
+ /// health: u32,
87
+ /// }
88
+ ///
89
+ /// impl Npc {
90
+ /// /// Construct a new NPC.
91
+ /// #[rune::function(path = Self::new)]
92
+ /// fn new(health: u32) -> Self {
93
+ /// Self {
94
+ /// health
95
+ /// }
96
+ /// }
97
+ ///
98
+ /// /// Damage the NPC with the given `amount`.
99
+ /// #[rune::function]
100
+ /// fn damage(&mut self, amount: u32) {
101
+ /// self.health -= amount;
102
+ /// }
103
+ /// }
104
+ ///
105
+ /// fn install() -> Result<rune::Module, rune::ContextError> {
106
+ /// let mut module = rune::Module::new();
107
+ /// module.ty::<Npc>()?;
108
+ /// module.function_meta(Npc::new)?;
109
+ /// module.function_meta(Npc::damage)?;
110
+ /// Ok(module)
73
111
/// }
74
112
/// ```
75
113
pub trait Any : Named + any:: Any {
76
114
/// The type hash of the type.
77
- ///
78
- /// TODO: make const field when `TypeId::of` is const.
79
115
fn type_hash ( ) -> Hash ;
80
116
}
81
117
0 commit comments