mrusty可以让你在mruby中使用Rust的结构和枚举并运行它们。
示例代码:
// mrf!#[macro_use]exter crate mrusty;// Needs some udocumeted, hidde calls.use mrusty::*;let mruby = MRuby::ew();struct Cot { value: i32}// Cot should ot flood the curret amespace. We will add it with require.impl MRubyFile for Cot { f require(mruby: MRubyType) { mruby.def_class::<Cot>("Cotaier"); // Coverts mruby types automatically & safely. // slf is always Value i iitialize(). mruby.def_method::<Cot, _>("iitialize", mrf!(|_mruby, slf: Value, v: i32| { let cot = Cot { value: v }; slf.iit(cot) })); mruby.def_method::<Cot, _>("value", mrf!(|mruby, slf: Cot| { mruby.fixum(slf.value) })); }}// Add file to the cotext, makig it requirable.mruby.def_file::<Cot>("cot");// Add spec testig.describe!(Cot, " cotext 'whe cotaiig 1' do it 'returs 1 whe callig #value' do expect(Cotaier.ew(1).value).to eql 1 ed ed");let result = mruby.ru(" require 'cot' Cotaier.ew(3).value").uwrap(); // Returs Value.pritl!("{}", result.to_i32().uwrap()); // Prits "3".
评论