[−][src]Trait rosy::Rosy
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.
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
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.