rune

Trait Any

Source
pub trait Any:
    TypeHash
    + Named
    + Any {
    const ANY_TYPE_INFO: AnyTypeInfo = _;
}
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]]: macro@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)
}

Provided Associated Constants§

Source

const ANY_TYPE_INFO: AnyTypeInfo = _

The compile-time type information know for the type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Any for Option<Value>

Source§

impl Any for Result<Value, Value>

Source§

impl Any for Error

Source§

impl Any for ParseCharError

Source§

impl Any for Error

Source§

impl Any for ParseFloatError

Source§

impl Any for ParseIntError

Source§

impl Any for Utf8Error

Source§

impl Any for Error

Available on crate feature std only.

Implementors§