Struct mrusty::Value [] [src]

pub struct Value { /* 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]

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);

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);

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);

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"));

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());

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

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");

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);

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);

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);

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");

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);

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());

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")
]);

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");

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]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq<Value> for Value
[src]

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

This method tests for !=.

impl Debug for Value
[src]

Formats the value using the given formatter.