1 unstable release
Uses old Rust 2015
0.3.0 | Oct 18, 2017 |
---|
#2253 in Rust patterns
30KB
581 lines
plugger-ruby
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 struct
s and
impl
s 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
eval
uating 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();
}
Dependencies
~20KB