pub trait Any: Named + Any {
// Required method
fn type_hash() -> Hash;
}
Expand description
Derive for types which can be used inside of Rune.
Rune only supports two types, built-in types String
and external
types which derive Any
. Before they can be used they must be registered in
Context::install
through a Module
.
This is typically used in combination with declarative macros to register functions and macros, such as:
- [
#[rune::function]
] - [
#[rune::macro_]
]
[#[rune::function]
]: crate::function
[#[rune::macro_]
]: crate::macro_
§Examples
use rune::Any;
#[derive(Any)]
struct Npc {
#[rune(get)]
health: u32,
}
impl Npc {
/// Construct a new NPC.
#[rune::function(path = Self::new)]
fn new(health: u32) -> Self {
Self {
health
}
}
/// Damage the NPC with the given `amount`.
#[rune::function]
fn damage(&mut self, amount: u32) {
self.health -= amount;
}
}
fn install() -> Result<rune::Module, rune::ContextError> {
let mut module = rune::Module::new();
module.ty::<Npc>()?;
module.function_meta(Npc::new)?;
module.function_meta(Npc::damage)?;
Ok(module)
}
Required Methods§
Object Safety§
This trait is not object safe.