Struct mrusty::Value [] [src]

pub struct Value {
    // some fields omitted
}

A struct that wraps around any mruby variable.

Values are created from the Mruby instance:

Examples

let mruby = Mruby::new();
let result = mruby.run("true").unwrap(); // Value

// Values need to be unwrapped in order to make sure they have the right mruby type.
assert_eq!(result.to_bool().unwrap(), true);

Methods

impl Value
[src]

fn init<T: Any>(self, obj: T) -> Value

Initializes the self mruby object passed to initialize with a Rust object of type T.

Note: T must be defined on the current Mruby with def_class.

Examples

use mrusty::{Mruby, MrubyImpl};

let mruby = Mruby::new();

struct Cont {
    value: i32
};

mruby.def_class_for::<Cont>("Container");
mruby.def_method_for::<Cont, _>("initialize", mrfn!(|_mruby, slf: Value, v: i32| {
    let cont = Cont { value: v };

    slf.init(cont) // Return the same slf value.
}));

let result = mruby.run("Container.new 3").unwrap();
let result = result.to_obj::<Cont>().unwrap();
let result = result.borrow();

assert_eq!(result.value, 3);

fn call(&self, name: &str, args: Vec<Value>) -> Result<ValueMrubyError>

Calls method name on a Value passing args.

Examples

let mruby = Mruby::new();

let one = mruby.fixnum(1);
let result = one.call("+", vec![mruby.fixnum(2)]).unwrap();

assert_eq!(result.to_i32().unwrap(), 3);

unsafe fn call_unchecked(&self, name: &str, args: Vec<Value>) -> Value

Calls method name on a Value passing args. If call fails, mruby will be left to handle the exception.

The method is unsafe because running it within a Rust context will interrupt drops, potentially leading to memory leaks.

Examples

let mruby = Mruby::new();

let one = mruby.fixnum(1);
let result = unsafe { one.call_unchecked("+", vec![mruby.fixnum(2)]) };

assert_eq!(result.to_i32().unwrap(), 3);

fn has_var(&self, name: &str) -> bool

Returns whether the instance variable name is defined on a Value.

Examples

let mruby = Mruby::new();

mruby.def_class("Container");

let cont = mruby.run("Container.new").unwrap();

assert!(!cont.has_var("value"));

fn get_var(&self, name: &str) -> Option<Value>

Returns the value of the instance variable name in a Some or None if it is not defined.

Examples

let mruby = Mruby::new();

mruby.def_class("Container");

let cont = mruby.run("Container.new").unwrap();

cont.set_var("value", mruby.fixnum(2));

assert_eq!(cont.get_var("value").unwrap().to_i32().unwrap(), 2);
assert!(cont.get_var("valup").is_none());

fn set_var(&self, name: &str, value: Value)

Sets the value of the instance variable name to value.

Examples

let mruby = Mruby::new();

mruby.def_class("Container");

let cont = mruby.run("Container.new").unwrap();

cont.set_var("value", mruby.fixnum(2));

assert!(cont.has_var("value"));
assert_eq!(cont.get_var("value").unwrap().to_i32().unwrap(), 2);


Method panics if called on non-objects.

let mruby = Mruby::new();

let one = mruby.fixnum(1);

one.set_var("value", mruby.fixnum(2)); // panics because Fixnum cannot have instance vars

fn class(&self) -> Class

Returns the Class of an mruby Value.

Examples

let mruby = Mruby::new();

let one = mruby.run("1").unwrap();
assert_eq!(one.class().to_str(), "Fixnum");

fn to_bool(&self) -> Result<boolMrubyError>

Casts a Value and returns a bool in an Ok or an Err if the types mismatch.

Example

let mruby = Mruby::new();
let result = mruby.run("
  def pos(n)
    n > 0
  end

  pos 1
").unwrap();

assert_eq!(result.to_bool().unwrap(), true);

fn to_i32(&self) -> Result<i32MrubyError>

Casts a Value and returns an i32 in an Ok or an Err if the types mismatch.

Example

let mruby = Mruby::new();
let result = mruby.run("
  def fact(n)
    n > 1 ? fact(n - 1) * n : 1
  end

  fact 5
").unwrap();

assert_eq!(result.to_i32().unwrap(), 120);

fn to_f64(&self) -> Result<f64MrubyError>

Casts a Value and returns an f64 in an Ok or an Err if the types mismatch.

Example

let mruby = Mruby::new();
let result = mruby.run("
  3 / 2.0
").unwrap();

assert_eq!(result.to_f64().unwrap(), 1.5);

fn to_str<'a>(&self) -> Result<&'a strMrubyError>

Casts a Value and returns a &str in an Ok or an Err if the types mismatch.

Example

let mruby = Mruby::new();
let result = mruby.run("
  [1, 2, 3].map(&:to_s).join
").unwrap();

assert_eq!(result.to_str().unwrap(), "123");
let mruby = Mruby::new();
let result = mruby.run(":symbol").unwrap();

assert_eq!(result.to_str().unwrap(), "symbol");

fn to_obj<T: Any>(&self) -> Result<Rc<RefCell<T>>, MrubyError>

Casts mruby Value of Class name to Rust type Rc<T>.

Note: T must be defined on the current Mruby with def_class.

Examples

let mruby = Mruby::new();

struct Cont {
    value: i32
}

mruby.def_class_for::<Cont>("Container");

let value = mruby.obj(Cont { value: 3 });
let cont = value.to_obj::<Cont>().unwrap();
let cont = cont.borrow();

assert_eq!(cont.value, 3);

fn to_option<T: Any>(&self) -> Result<Option<Rc<RefCell<T>>>, MrubyError>

Casts mruby Value of Class name to Rust Option of Rc<T>.

Note: T must be defined on the current Mruby with def_class.

Examples

let mruby = Mruby::new();

struct Cont {
    value: i32
}

mruby.def_class_for::<Cont>("Container");

let value = mruby.obj(Cont { value: 3 });
let cont = value.to_option::<Cont>().unwrap().unwrap();
let cont = cont.borrow();

assert_eq!(cont.value, 3);
assert!(mruby.nil().to_option::<Cont>().unwrap().is_none());

fn to_vec(&self) -> Result<Vec<Value>, MrubyError>

Casts mruby Value of Class Array to Rust type Vec<Value>.

Examples

let mruby = Mruby::new();
let result = mruby.run("
  [1, 2, 3].map(&:to_s)
").unwrap();

assert_eq!(result.to_vec().unwrap(), vec![
    mruby.string("1"),
    mruby.string("2"),
    mruby.string("3")
]);

fn to_class(&self) -> Result<ClassMrubyError>

Casts mruby Value of Class Class to Rust type Class.

Examples

let mruby = Mruby::new();
let result = mruby.run("Object").unwrap();

assert_eq!(result.to_class().unwrap().to_str(), "Object");

fn to_module(&self) -> Result<ModuleMrubyError>

Casts mruby Value of Class Module to Rust type Module.

Examples

let mruby = Mruby::new();
let result = mruby.run("Kernel").unwrap();

assert_eq!(result.to_module().unwrap().to_str(), "Kernel");

Trait Implementations

impl Clone for Value
[src]

fn clone(&self) -> Value

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more

impl PartialEq<Value> for Value
[src]

fn eq(&self, other: &Value) -> bool

This method tests for self and other values to be equal, and is used by ==. Read more

fn ne(&self, other: &Rhs) -> bool
1.0.0

This method tests for !=.

impl Debug for Value
[src]

fn fmt(&self, f: &mut Formatter) -> Result

Formats the value using the given formatter.