plugger

Crates.ioplugger
lib.rsplugger
version0.3.0
sourcesrc
created_at2017-10-18 05:38:11.864818
updated_at2017-10-18 05:38:11.864818
descriptionPlugger
homepage
repositoryhttps://github.com/dylanmckay/plugger
max_upload_size
id36026
size24,390
Dylan McKay (dylanmckay)

documentation

README

plugger-ruby

Build Status Crates.io MIT licensed

Embed Ruby plugins directly into your Rust project!

Requires Rust nightly.

Purpose

The purpose of this library is to allow scripting in your Rust projects as easy as possible.

The library itself consists of two main parts - a Ruby VM and a syntax extension which creates Ruby wrappers over your structs and impls so they can be used directly from Ruby.

It should be possible to simply annotate a type with #[pluggable] and use it directly from Ruby.

The thing that separates the library from the others is that it allows you to share your Rust code with Ruby, as opposed to writing Ruby objects in Rust.

Features

  • Creation of a Ruby VM and evaluating Ruby code
  • Calling methods on Rust objects from Ruby
  • Accessing public struct fields from Ruby
  • Creating new Rust objects via Ruby
  • Complicated types such as enums, tuples
  • Automatic marshalling of Ruby arguments into Rust types
  • Automatic marshalling of Rust return types into Ruby values
  • Support for Python

Example

NOTE: Not everything here is supported yet. This is mostly automatic coercion between Rust and Ruby types.

Check tools/ for a working example.

struct Vector3(pub f64, pub f64, pub f64);

#[pluggable]
struct Player
{
    name: String,

    health: f32,

    position: Vector3,
    rotation: Vector3,
}

#[pluggable]
impl Player
{
    pub fn revive(&mut self) { self.health = 1.0 }
    pub fn rename(&mut self, name: &str) { self.name = name.to_owned() }

    pub fn transport(&mut self, position: Vector3) { self.position = position; }
}

fn main() {
    let mut vm = Ruby::new();

    let player = Player { /* ... */ };
    vm.plug("main_player", player);

    vm.eval("main_player.revive").unwrap();
    vm.eval("main_player.rename('foo')").unwrap();
}
Commit count: 25

cargo fmt