@@ -3,6 +3,86 @@ use core::any;
3
3
use crate :: compile:: Named ;
4
4
use crate :: runtime:: { AnyTypeInfo , TypeHash } ;
5
5
6
+ /// The trait implemented for types which can be used inside of Rune.
7
+ ///
8
+ /// This can only be implemented correctly through the [`Any`] derive.
9
+ /// Implementing it manually is not supported.
10
+ ///
11
+ /// Rune only supports two types, *built-in* types like [`i64`] and *external*
12
+ /// types which derive `Any`. Before they can be used they must be registered in
13
+ /// [`Context::install`] through a [`Module`].
14
+ ///
15
+ /// This is typically used in combination with declarative macros to register
16
+ /// functions and macros, such as [`rune::function`].
17
+ ///
18
+ /// [`AnyObj`]: crate::runtime::AnyObj
19
+ /// [`Context::install`]: crate::Context::install
20
+ /// [`Module`]: crate::Module
21
+ /// [`String`]: std::string::String
22
+ /// [`rune::function`]: macro@crate::function
23
+ /// [`rune::macro_`]: macro@crate::macro_
24
+ /// [`Any`]: derive@crate::Any
25
+ ///
26
+ /// # Examples
27
+ ///
28
+ /// ```
29
+ /// use rune::Any;
30
+ ///
31
+ /// #[derive(Any)]
32
+ /// struct Npc {
33
+ /// #[rune(get)]
34
+ /// health: u32,
35
+ /// }
36
+ ///
37
+ /// impl Npc {
38
+ /// /// Construct a new NPC.
39
+ /// #[rune::function(path = Self::new)]
40
+ /// fn new(health: u32) -> Self {
41
+ /// Self {
42
+ /// health
43
+ /// }
44
+ /// }
45
+ ///
46
+ /// /// Damage the NPC with the given `amount`.
47
+ /// #[rune::function]
48
+ /// fn damage(&mut self, amount: u32) {
49
+ /// self.health -= amount;
50
+ /// }
51
+ /// }
52
+ ///
53
+ /// fn install() -> Result<rune::Module, rune::ContextError> {
54
+ /// let mut module = rune::Module::new();
55
+ /// module.ty::<Npc>()?;
56
+ /// module.function_meta(Npc::new)?;
57
+ /// module.function_meta(Npc::damage)?;
58
+ /// Ok(module)
59
+ /// }
60
+ /// ```
61
+ pub trait Any : TypeHash + Named + any:: Any {
62
+ /// The compile-time type information know for the type.
63
+ const ANY_TYPE_INFO : AnyTypeInfo = AnyTypeInfo :: new ( Self :: full_name, Self :: HASH ) ;
64
+ }
65
+
66
+ /// Trait implemented for types which can be automatically converted to a
67
+ /// [`Value`].
68
+ ///
69
+ /// We can't use a blanked implementation over `T: Any` because it only governs
70
+ /// what can be stored in any [`AnyObj`].
71
+ ///
72
+ /// This trait in contrast is selectively implemented for types which we want to
73
+ /// generate [`ToValue`] and [`FromValue`] implementations for.
74
+ ///
75
+ /// [`Value`]: crate::runtime::Value
76
+ /// [`AnyObj`]: crate::runtime::AnyObj
77
+ /// [`ToValue`]: crate::runtime::ToValue
78
+ /// [`FromValue`]: crate::runtime::FromValue
79
+ ///
80
+ /// Note that you are *not* supposed to implement this directly. Make use of the
81
+ /// [`Any`] derive instead.
82
+ ///
83
+ /// [`Any`]: derive@crate::Any
84
+ pub trait AnyMarker : Any { }
85
+
6
86
/// Macro to mark a value as external, which will implement all the appropriate
7
87
/// traits.
8
88
///
@@ -226,82 +306,3 @@ use crate::runtime::{AnyTypeInfo, TypeHash};
226
306
/// [`rune::alloc`]: crate::alloc
227
307
/// [`TryClone` trait]: crate::alloc::clone::TryClone
228
308
pub use rune_macros:: Any ;
229
-
230
- /// Derive for types which can be used inside of Rune.
231
- ///
232
- /// Rune only supports two types, *built-in* types [`String`] and *external*
233
- /// types which derive `Any`. Before they can be used they must be registered in
234
- /// [`Context::install`] through a [`Module`].
235
- ///
236
- /// This is typically used in combination with declarative macros to register
237
- /// functions and macros, such as:
238
- ///
239
- /// * [`#[rune::function]`]
240
- /// * [`#[rune::macro_]`]
241
- ///
242
- /// [`AnyObj`]: crate::runtime::AnyObj
243
- /// [`Context::install`]: crate::Context::install
244
- /// [`Module`]: crate::Module
245
- /// [`String`]: std::string::String
246
- /// [`#[rune::function]`]: macro@crate::function
247
- /// [`#[rune::macro_]`]: crate::macro_
248
- ///
249
- /// # Examples
250
- ///
251
- /// ```
252
- /// use rune::Any;
253
- ///
254
- /// #[derive(Any)]
255
- /// struct Npc {
256
- /// #[rune(get)]
257
- /// health: u32,
258
- /// }
259
- ///
260
- /// impl Npc {
261
- /// /// Construct a new NPC.
262
- /// #[rune::function(path = Self::new)]
263
- /// fn new(health: u32) -> Self {
264
- /// Self {
265
- /// health
266
- /// }
267
- /// }
268
- ///
269
- /// /// Damage the NPC with the given `amount`.
270
- /// #[rune::function]
271
- /// fn damage(&mut self, amount: u32) {
272
- /// self.health -= amount;
273
- /// }
274
- /// }
275
- ///
276
- /// fn install() -> Result<rune::Module, rune::ContextError> {
277
- /// let mut module = rune::Module::new();
278
- /// module.ty::<Npc>()?;
279
- /// module.function_meta(Npc::new)?;
280
- /// module.function_meta(Npc::damage)?;
281
- /// Ok(module)
282
- /// }
283
- /// ```
284
- pub trait Any : TypeHash + Named + any:: Any {
285
- /// The compile-time type information know for the type.
286
- const ANY_TYPE_INFO : AnyTypeInfo = AnyTypeInfo :: new ( Self :: full_name, Self :: HASH ) ;
287
- }
288
-
289
- /// Trait implemented for types which can be automatically converted to a
290
- /// [`Value`].
291
- ///
292
- /// We can't use a blanked implementation over `T: Any` because it only governs
293
- /// what can be stored in any [`AnyObj`].
294
- ///
295
- /// This trait in contrast is selectively implemented for types which we want to
296
- /// generate [`ToValue`] and [`FromValue`] implementations for.
297
- ///
298
- /// [`Value`]: crate::runtime::Value
299
- /// [`AnyObj`]: crate::runtime::AnyObj
300
- /// [`ToValue`]: crate::runtime::ToValue
301
- /// [`FromValue`]: crate::runtime::FromValue
302
- ///
303
- /// Note that you are *not* supposed to implement this directly. Make use of the
304
- /// [`Any`] derive instead.
305
- ///
306
- /// [`Any`]: derive@Any
307
- pub trait AnyMarker : Any { }
0 commit comments