8000 Improve Any derive documentation · rune-rs/rune@4aa9e1f · GitHub
[go: up one dir, main page]

Skip to content

Commit 4aa9e1f

Browse files
committed
Improve Any derive documentation
1 parent fea782a commit 4aa9e1f

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

crates/rune/src/any.rs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,62 @@ use crate::hash::Hash;
5656
/// ```
5757
pub use rune_macros::Any;
5858

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.
6060
///
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`].
6464
///
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
6679
///
6780
/// ```
6881
/// use rune::Any;
6982
///
7083
/// #[derive(Any)]
7184
/// 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)
73111
/// }
74112
/// ```
75113
pub trait Any: Named + any::Any {
76114
/// The type hash of the type.
77-
///
78-
/// TODO: make const field when `TypeId::of` is const.
79115
fn type_hash() -> Hash;
80116
}
81117

crates/rune/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub mod alloc;
184184
#[cfg(test)]
185185
pub(crate) mod testing;
186186

187-
/// Helper prelude for #[no_std] support.
187+
/// Helper prelude for `#[no_std]` support.
188188
pub mod no_std;
189189

190190
#[macro_use]
@@ -200,6 +200,7 @@ pub mod ast;
200200
pub mod fmt;
201201

202202
cfg_emit! {
203+
#[doc(inline)]
203204
pub use ::codespan_reporting::term::termcolor;
204205
}
205206

@@ -243,15 +244,18 @@ pub mod parse;
243244
pub mod query;
244245

245246
pub mod runtime;
247+
#[doc(inline)]
246248
pub use self::runtime::{from_value, to_value, FromValue, ToValue, Unit, Value, Vm};
247249

248250
mod shared;
249251

250252
pub mod source;
253+
#[doc(inline)]
251254
pub use self::source::Source;
252255

253256
#[macro_use]
254257
mod sources;
258+
#[doc(inline)]
255259
pub use self::sources::{SourceId, Sources};
256260

257261
mod worker;

0 commit comments

Comments
 (0)
0