mrusty 0.3.0

mruby safe bindings for Rust
docs.rs failed to build mrusty-0.3.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: mrusty-1.0.0

mrusty. mruby safe bindings for Rust

[Build Status] (https://travis-ci.org/anima-engine/mrusty) [![Coverage Status] (https://coveralls.io/repos/github/anima-engine/mrusty/badge.svg?branch=master)] (https://coveralls.io/github/anima-engine/mrusty?branch=master)

mrusty lets you reflect Rust structs and enums in mruby and run them. It does this in a safely neat way while also bringing spec testing and a REPL to the table.

Note: Starting with v0.3.0, mrusty will only work with Rust nightly. This is caused by a need to capture panics in mruby. Once this features stabilizes (and it will in Rust 1.9.0), mrusty will return to stable Rust.

Requirements

mrusty requires mruby compiled with -fPIC. To compile and install mruby 1.2.0:

  • make sure you have Bison & Ruby installed
  • download the source
  • unzip and cd to mruby-1.2.0/
  • add the following lines to build_config.rb as in the # C compiler settings example (make sure you add it after the comment):
conf.cc do |cc|
  cc.flags << '-fPIC'
end
  • run ./minirake
  • copy header files from include to /usr/loca/include
  • copy build/host/lib/libmruby.a to /usr/local/lib

Note: On OSX you can install it from homebrew with brew install mruby.

Documentation

Example

A very simple example of a Container struct which will be passed to mruby and which is perfectly callable.

// mrfn!
#[macro_use]
extern crate mrusty;

// Needs some undocumented, hidden calls.
use mrusty::*;

let mruby = Mruby::new();

struct Cont {
    value: i32
}

// Cont should not flood the current namespace. We will add it with require.
mrclass!(Cont, "Container", {
    // Converts mruby types automatically & safely.
    def!("initialize", |v: i32| {
        Cont { value: v }
    });

    // Converts slf to Cont.
    def!("value", |mruby, slf: Cont| {
        mruby.fixnum(slf.value)
    });
});

// Add file to the context, making it requirable.
mruby.def_file::<Cont>("cont");

// Add spec testing.
describe!(Cont, "
  context 'when containing 1' do
    it 'returns 1 when calling #value' do
      expect(Container.new(1).value).to eql 1
    end
  end
");

let result = mruby.run("
    require 'cont'

    Container.new(3).value
").unwrap(); // Returns Value.

println!("{}", result.to_i32().unwrap()); // Prints "3".