Struct magnus::typed_data::Obj
source · pub struct Obj<T> { /* private fields */ }
Expand description
A Ruby Object wrapping a Rust type T
.
This is a Value pointer to a RTypedData struct, Ruby’s internal
representation of objects that wrap foreign types. Unlike RTypedData
it
tracks the Rust type it should contains and errors early in TryConvert
if types don’t match, rather than on Obj::get
.
See the ReprValue
and Object
traits for additional methods
available on this type. See Ruby
for methods to
create a typed_data::Obj
.
Implementations§
source§impl<T> Obj<T>where
T: TypedData,
impl<T> Obj<T>where
T: TypedData,
sourcepub fn wrap(data: T) -> Self
pub fn wrap(data: T) -> Self
Wrap the Rust type T
in a Ruby object.
§Panics
Panics if called from a non-Ruby thread. See Ruby::obj_wrap
for the
non-panicking version.
§Examples
use magnus::{class, define_class, prelude::*, typed_data};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
let point_class = define_class("Point", class::object()).unwrap();
let value = typed_data::Obj::wrap(Point { x: 4, y: 2 });
assert!(value.is_kind_of(point_class));
sourcepub fn wrap_as(data: T, class: RClass) -> Self
pub fn wrap_as(data: T, class: RClass) -> Self
Wrap the Rust type T
in a Ruby object that is an instance of the
given class
.
See also TypedData::class_for
.
§Panics
Panics if class
is not a subclass of <T as TypedData>::class()
, or
if called from a non-Ruby thread. See Ruby::obj_wrap_as
for a
version that can not be called from a non-Ruby thread, so will not
panic for that reason.
§Examples
use magnus::{class, define_class, prelude::*, typed_data};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
let point_class = define_class("Point", class::object()).unwrap();
let point_sub_class = define_class("SubPoint", point_class).unwrap();
let value = typed_data::Obj::wrap_as(Point { x: 4, y: 2 }, point_sub_class);
assert!(value.is_kind_of(point_sub_class));
assert!(value.is_kind_of(point_class));
Allowing a wrapped type to be subclassed from Ruby:
(note, in this example Point
does not have and does not call
the initialize
method, subclasses would need to override the class
new
method rather than initialize
)
use magnus::{
class, define_class, eval, function, method, prelude::*, typed_data, RClass, Value,
};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
impl Point {
fn new(class: RClass, x: isize, y: isize) -> typed_data::Obj<Self> {
typed_data::Obj::wrap_as(Self { x, y }, class)
}
}
let point_class = define_class("Point", class::object()).unwrap();
point_class
.define_singleton_method("new", method!(Point::new, 2))
.unwrap();
point_class
.define_singleton_method("inherited", function!(RClass::undef_default_alloc_func, 1))
.unwrap();
let value: Value = eval(
r#"
class SubPoint < Point
end
SubPoint.new(4, 2)
"#,
)
.unwrap();
assert!(value.is_kind_of(class::object().const_get::<_, RClass>("SubPoint").unwrap()));
assert!(value.is_kind_of(point_class));
Trait Implementations§
source§impl<T> Deref for Obj<T>where
T: TypedData,
impl<T> Deref for Obj<T>where
T: TypedData,
source§fn deref(&self) -> &Self::Target
fn deref(&self) -> &Self::Target
Dereference to the Rust type wrapped in the Ruby object self
.
§Examples
use magnus::{class, define_class, typed_data};
#[magnus::wrap(class = "Point")]
#[derive(Debug, PartialEq, Eq)]
struct Point {
x: isize,
y: isize,
}
define_class("Point", class::object()).unwrap();
let value = typed_data::Obj::wrap(Point { x: 4, y: 2 });
assert_eq!(&*value, &Point { x: 4, y: 2 });
source§impl<T> Object for Obj<T>where
T: TypedData,
impl<T> Object for Obj<T>where
T: TypedData,
source§fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
fn define_singleton_method<M>(self, name: &str, func: M) -> Result<(), Error>where
M: Method,
self
’s scope. Read moresource§impl<T> ReprValue for Obj<T>where
T: TypedData,
impl<T> ReprValue for Obj<T>where
T: TypedData,
source§fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn equal<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
#==
. Read moresource§fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
fn eql<T>(self, other: T) -> Result<bool, Error>where
T: ReprValue,
#eql?
. Read moresource§fn hash(self) -> Result<Integer, Error>
fn hash(self) -> Result<Integer, Error>
self
. Read moresource§fn to_bool(self) -> bool
fn to_bool(self) -> bool
self
to a bool
, following Ruby’s rules of false
and nil
as boolean false
and everything else boolean true
. Read moresource§fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
source§fn block_call<M, A, R, T>(
self,
method: M,
args: A,
block: fn(_: &[Value], _: Option<Proc>) -> R
) -> Result<T, Error>
fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(_: &[Value], _: Option<Proc>) -> R ) -> Result<T, Error>
source§fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
fn respond_to<M>(self, method: M, include_private: bool) -> Result<bool, Error>where
M: IntoId,
self
responds to the given Ruby method. Read more