Struct magnus::r_typed_data::RTypedData
source · #[repr(transparent)]pub struct RTypedData(_);
Expand description
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.
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.
Examples
use magnus::{define_class, RTypedData};
#[magnus::wrap(class = "Point")]
struct Point {
x: isize,
y: isize,
}
let point_class = define_class("Point", Default::default()).unwrap();
let value = RTypedData::wrap(Point { x: 4, y: 2 });
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::{define_class, RTypedData};
#[magnus::wrap(class = "Point")]
#[derive(Debug, PartialEq, Eq)]
struct Point {
x: isize,
y: isize,
}
let point_class = define_class("Point", Default::default()).unwrap();
let value = RTypedData::wrap(Point { x: 4, y: 2 });
assert_eq!(value.get::<Point>().unwrap(), &Point { x: 4, y: 2 });
Methods from Deref<Target = Value>§
sourcepub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
pub unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>
Convert self
to a Rust string.
Safety
This may return a direct view of memory owned and managed by Ruby. Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, QTRUE};
let value = QTRUE;
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.to_s() }.unwrap().into_owned();
assert_eq!(s, "true");
sourcepub unsafe fn classname(&self) -> Cow<'_, str>
pub unsafe fn classname(&self) -> Cow<'_, str>
Return the name of self
’s class.
Safety
Ruby may modify or free the memory backing the returned str, the caller must ensure this does not happen.
This can be used safely by immediately calling
into_owned
on the return value.
Examples
use magnus::{eval, RHash};
let value = RHash::new();
// safe as we neve give Ruby a chance to free the string.
let s = unsafe { value.classname() }.into_owned();
assert_eq!(s, "Hash");
Trait Implementations§
source§impl Clone for RTypedData
impl Clone for RTypedData
source§fn clone(&self) -> RTypedData
fn clone(&self) -> RTypedData
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for RTypedData
impl Debug for RTypedData
source§impl Deref for RTypedData
impl Deref for RTypedData
source§impl Display for RTypedData
impl Display for RTypedData
source§impl From<RTypedData> for Value
impl From<RTypedData> for Value
source§fn from(val: RTypedData) -> Self
fn from(val: RTypedData) -> Self
Converts to this type from the input type.
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,
Define a singleton method in
self
’s scope. Read more