Struct magnus::RTypedData
source · pub struct RTypedData(/* private fields */);
Expand description
A Value pointer to a RTypedData struct, Ruby’s internal representation of objects that wrap foreign types.
See also typed_data::Obj
.
See the ReprValue
and Object
traits for additional methods
available on this type. See Ruby
for methods to create
an RTypedData
.
Implementations§
source§impl RTypedData
impl RTypedData
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RTypedData)
if val
is a RTypedData
, None
otherwise.
§Examples
use magnus::{class, define_class, eval, function, prelude::*, RTypedData};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
let point_class = define_class("Point", class::object()).unwrap();
point_class
.define_singleton_method("new", function!(|x, y| Point { x, y }, 2))
.unwrap();
assert!(RTypedData::from_value(eval(r#"Point.new(1, 2)"#).unwrap()).is_some());
assert!(RTypedData::from_value(eval(r#"Object.new"#).unwrap()).is_none());
sourcepub fn wrap<T>(data: T) -> Selfwhere
T: TypedData,
pub fn wrap<T>(data: T) -> Selfwhere
T: TypedData,
Wrap the Rust type T
in a Ruby object.
§Panics
Panics if called from a non-Ruby thread. See Ruby::wrap
for the
non-panicking version.
§Examples
use magnus::{class, define_class, prelude::*, RTypedData};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
let point_class = define_class("Point", class::object()).unwrap();
let value = RTypedData::wrap(Point { x: 4, y: 2 });
assert!(value.is_kind_of(point_class));
sourcepub fn wrap_as<T>(data: T, class: RClass) -> Selfwhere
T: TypedData,
pub fn wrap_as<T>(data: T, class: RClass) -> Selfwhere
T: TypedData,
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::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::*, RTypedData};
#[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 = RTypedData::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::*, RClass, RTypedData, Value,
};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
impl Point {
fn new(class: RClass, x: isize, y: isize) -> RTypedData {
RTypedData::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));
sourcepub fn get<T>(&self) -> Result<&T, Error>where
T: TypedData,
pub fn get<T>(&self) -> Result<&T, Error>where
T: TypedData,
Get a reference to the Rust type wrapped in the Ruby object self
.
§Examples
use magnus::{class, define_class, RTypedData};
#[magnus::wrap(class = "Point")]
#[derive(Debug, PartialEq, Eq)]
struct Point {
x: isize,
y: isize,
}
define_class("Point", class::object()).unwrap();
let value = RTypedData::wrap(Point { x: 4, y: 2 });
assert_eq!(value.get::<Point>().unwrap(), &Point { x: 4, y: 2 });
Trait Implementations§
source§impl Clone for RTypedData
impl Clone for RTypedData
source§fn clone(&self) -> RTypedData
fn clone(&self) -> RTypedData
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for RTypedData
impl Debug for RTypedData
source§impl Display for RTypedData
impl Display for RTypedData
source§impl IntoValue for RTypedData
impl IntoValue for RTypedData
source§impl Object for RTypedData
impl Object for RTypedData
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 ReprValue for RTypedData
impl ReprValue for RTypedData
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