Trait magnus::r_typed_data::TypedData
source · [−]pub unsafe trait TypedData where
Self: Sized, {
fn class() -> RClass;
fn data_type() -> &'static DataType;
}
Expand description
A trait for Rust types that can be used with the rb_data_typed_object_wrap
API.
Safety
This trait is unsafe to impliment as the fields of DataType
returned by
TypedData::data_type
control low level behaviour that can go very wrong
if set incorrectly. Implimenting this trait is the only way a DataType
can be passed to Ruby and result in safety violations, DataType
is
otherwise safe (but useless) to create.
The TypedData
or wrap
macros can
help implementing this trait more safely.
Required Methods
Should return the class for the Ruby object wrapping the Rust type.
Examples
use magnus::{define_class, memoize, RClass};
fn class() -> RClass {
*memoize!(RClass: define_class("Foo", Default::default()).unwrap())
}
Should return a static reference to a DataType
with metadata about
the wrapped type.
Examples
use magnus::{memoize, r_typed_data::DataTypeBuilder, DataType, DataTypeFunctions};
#[derive(DataTypeFunctions)]
struct Foo();
fn data_type() -> &'static DataType {
memoize!(DataType: DataTypeBuilder::<Foo>::new("foo").build())
}