[][src]Struct mruby::state::State

pub struct State {
    pub mrb: *mut mrb_state,
    pub ctx: *mut mrbc_context,
    pub vfs: MrbFilesystem,
    pub num_set_regexp_capture_globals: usize,
    // some fields omitted
}

Fields

mrb: *mut mrb_statectx: *mut mrbc_contextvfs: MrbFilesystemnum_set_regexp_capture_globals: usize

Methods

impl State[src]

pub fn new(
    mrb: *mut mrb_state,
    ctx: *mut mrbc_context,
    vfs: MrbFilesystem
) -> Self
[src]

pub fn close(self)[src]

Close a State and free underlying mruby structs and memory.

pub fn def_class<T: Any>(
    &mut self,
    name: &str,
    enclosing_scope: Option<EnclosingRubyScope>,
    free: Option<Free>
) -> Rc<RefCell<Spec>>
[src]

Create a class definition bound to a Rust type T. Class definitions have the same lifetime as the State because the class def owns the mrb_data_type for the type, which must be long-lived. Class defs are stored by TypeId of T.

Internally, class::Specs are stored in an Rc<RefCell<_>> which allows class specs to have multiple owners, such as being a super class or an enclosing scope for a class or a module. To mutate the class spec, call borrow_mut on the return value of this method to get a mutable reference to the class spec.

Class specs can also be retrieved from the state after creation with State::class_spec.

The recommended pattern for using def_class looks like this:

#[macro_use]
extern crate mruby;

use mruby::convert::FromMrb;
use mruby::def::{ClassLike, Define};
use mruby::sys;
use mruby::value::Value;

extern "C" fn value(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value
{
    let interp = unsafe { unwrap_interpreter!(mrb) };
    Value::from_mrb(&interp, 29).inner()
}

fn main() {
    let interp = mruby::interpreter().expect("mrb init");
    let spec = {
        let mut api = interp.borrow_mut();
        let spec = api.def_class::<()>("Container", None, None);
        spec.borrow_mut().add_method("value", value, sys::mrb_args_none());
        spec.borrow_mut().add_self_method("value", value, sys::mrb_args_none());
        spec.borrow_mut().mrb_value_is_rust_backed(true);
        spec
    };
    spec.borrow().define(&interp).expect("class install");
}

pub fn class_spec<T: Any>(&self) -> Option<Rc<RefCell<Spec>>>[src]

Retrieve a class definition from the state bound to Rust type T.

This function returns None if type T has not had a class spec registered for it using State::def_class.

Internally, class::Specs are stored in an Rc<RefCell<_>> which allows class specs to have multiple owners, such as being a super class or an enclosing scope for a class or a module. To mutate the class spec, call borrow_mut on the return value of this method to get a mutable reference to the class spec.

pub fn def_module<T: Any>(
    &mut self,
    name: &str,
    enclosing_scope: Option<EnclosingRubyScope>
) -> Rc<RefCell<Spec>>
[src]

Create a module definition bound to a Rust type T. Module definitions have the same lifetime as the State. Module defs are stored by TypeId of T.

Internally, module::Specs are stored in an Rc<RefCell<_>> which allows module specs to have multiple owners, such as being an enclosing scope for a class or a module. To mutate the module spec, call borrow_mut on the return value of this method to get a mutable reference to the module spec.

Module specs can also be retrieved from the state after creation with State::module_spec.

The recommended pattern for using def_module looks like this:

#[macro_use]
extern crate mruby;

use mruby::convert::FromMrb;
use mruby::def::{ClassLike, Define};
use mruby::sys;
use mruby::value::Value;

extern "C" fn value(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value
{
    let interp = unsafe { unwrap_interpreter!(mrb) };
    Value::from_mrb(&interp, 29).inner()
}

fn main() {
    let interp = mruby::interpreter().expect("mrb init");
    let spec = {
        let mut api = interp.borrow_mut();
        let spec = api.def_module::<()>("Container", None);
        spec.borrow_mut().add_method("value", value, sys::mrb_args_none());
        spec.borrow_mut().add_self_method("value", value, sys::mrb_args_none());
        spec
    };
    spec.borrow().define(&interp).expect("class install");
}

pub fn module_spec<T: Any>(&self) -> Option<Rc<RefCell<Spec>>>[src]

Retrieve a module definition from the state bound to Rust type T.

This function returns None if type T has not had a class spec registered for it using State::def_module.

Internally, module::Specs are stored in an Rc<RefCell<_>> which allows module specs to have multiple owners, such as being an enclosing scope for a class or a module. To mutate the module spec, call borrow_mut on the return value of this method to get a mutable reference to the module spec.

pub fn sym_intern(&mut self, sym: &str) -> mrb_sym[src]

Trait Implementations

impl Drop for State[src]

impl Display for State[src]

impl Debug for State[src]

Auto Trait Implementations

impl !Send for State

impl Unpin for State

impl !Sync for State

impl !UnwindSafe for State

impl !RefUnwindSafe for State

Blanket Implementations

impl<T> From<T> for T[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]