mrusty::mrusty_class!
[−]
[src]
macro_rules! mrusty_class { ( $name:ty ) => { ... }; ( $name:ty, { $( $rest:tt )* } ) => { ... }; ( $name:ty, $mrname:expr ) => { ... }; ( $name:ty, $mrname:expr, { $( $rest:tt )* } ) => { ... }; }
A macro that comes in handy when defining an mruby Class with Rust type reflection. It
automates and simplifies the implementation of the MrubyFile trait. Thus, any type provided
to mrusty_class! will get an MrubyFile implementation, unlike mruby_class! which
implements a pure mruby class.
The macro takes a Rust type, an optional mruby Class name, and a block as arguments. Inside
of the block you can define mruby methods with the def! and def_self! helpers which are
not visible outside of this macro.
def! and def_self! are analogous to mrfn! which has more usage examples.
Examples
Use def! to define mruby instance methods. "initialize" is a special type of instance
method which require you to return an initialized type. Apart from this, all methods require
you to return a Value. Apart from that, "initialize" is the only method that doesn't
take itself as an argument.
Note: mruby argument is optional.
use mrusty::{Mruby, MrubyFile, MrubyImpl}; let mruby = Mruby::new(); struct Cont { value: i32 }; mrusty_class!(Cont, "Container", { def!("initialize", |v: i32| { Cont { value: v } }); def!("value", |mruby, slf: (&Cont)| { mruby.fixnum(slf.value) }); }); Cont::require(mruby.clone()); // needs to be required manually let result = mruby.run("Container.new(3).value").unwrap(); assert_eq!(result.to_i32().unwrap(), 3);
Use def_self! to define mruby class methods.
Note: mruby argument is optional.
use mrusty::{Mruby, MrubyFile, MrubyImpl}; let mruby = Mruby::new(); struct Cont { value: i32 }; mrusty_class!(Cont, "Container", { def_self!("hi", |mruby, slf: Value| { mruby.string("hi") }); }); Cont::require(mruby.clone()); // needs to be required manually let result = mruby.run("Container.hi").unwrap(); assert_eq!(result.to_str().unwrap(), "hi");