[][src]Trait rosy::Rosy

pub unsafe trait Rosy: Sized {
const ID: *const c_char;

    fn mark(&self);

    fn unique_object_id() -> Option<u128> { ... }
fn class() -> Class { ... }
fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>> { ... }
fn free(self: Box<Self>) { ... }
fn size(&self) -> usize { ... } }

A Rust type that can be used as a object.

Examples

Implementing this trait allows for wrapping Rust data in a RosyObject.

use std::os::raw::c_char;
use rosy::{Rosy, RosyObject, Class};

struct Flag<'a> {
    did_free: &'a mut bool
}

unsafe impl Rosy for Flag<'_> {
    const ID: *const c_char = "rosy_flag\0".as_ptr() as _;

    fn class() -> Class {
        Class::get_or_def("RosyFlag").unwrap()
    }

    fn mark(&self) {}

    fn free(self: Box<Self>) {
        *self.did_free = true;
    }
}

let mut did_free = false;
let obj = RosyObject::from(Flag { did_free: &mut did_free });

unsafe { rosy::vm::destroy() };

assert!(did_free);

Associated Constants

const ID: *const c_char

A C string of the unique identifier of the type.

Note that the type is not CStr. This is because creating a constant instance can only be done on nightly.

Loading content...

Required methods

fn mark(&self)

Called during Ruby's mark phase of garbage collection to determine which Ruby references in self are live and should not be swept.

Safety

This method is called during garbage collection and it is required that:

  • All live Ruby objects are properly marked
  • No new Ruby objects are allocated
Loading content...

Provided methods

fn unique_object_id() -> Option<u128>

A unique identifier for RosyObject<Self> to facilitate casting.

Safety

This value must be unique. Rosy's built-in objects use identifiers that are very close to u128::max_value(), so those are easy to avoid.

fn class() -> Class

Returns the class defined for this type.

The default is RustObject, however other implementors of this trait should consider using a different class to define methods or properties on.

fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>>

Attempts to create a RosyObject instance by casting obj.

This could be implemented by checking against class but care must be taken to ensure that all instances of this type's class refer to Rust data of type Self.

The default implementation checks the unique_object_id of Self against the unique_id of A.

fn free(self: Box<Self>)

Runs destructors and frees self.

Safety

The implementor must ensure that no new Ruby objects are allocated.

fn size(&self) -> usize

Returns the estimated memory consumption of self in bytes.

Loading content...

Implementations on Foreign Types

impl<R: Rosy, '_> Rosy for &'_ [R][src]

fn unique_object_id() -> Option<u128>[src]

fn class() -> Class[src]

fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>>[src]

fn free(self: Box<Self>)[src]

impl<R: Rosy, '_> Rosy for &'_ mut [R][src]

fn unique_object_id() -> Option<u128>[src]

fn class() -> Class[src]

fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>>[src]

fn free(self: Box<Self>)[src]

impl<R: Rosy> Rosy for Vec<R>[src]

fn class() -> Class[src]

fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>>[src]

fn free(self: Box<Self>)[src]

impl<'_> Rosy for &'_ str[src]

fn unique_object_id() -> Option<u128>[src]

fn class() -> Class[src]

fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>>[src]

fn free(self: Box<Self>)[src]

impl Rosy for String[src]

fn class() -> Class[src]

fn cast<A: Object>(obj: A) -> Option<RosyObject<Self>>[src]

fn free(self: Box<Self>)[src]

Loading content...

Implementors

Loading content...