pub struct RStruct(/* private fields */);
Expand description
Implementations§
source§impl RStruct
impl RStruct
sourcepub fn from_value(val: Value) -> Option<Self>
pub fn from_value(val: Value) -> Option<Self>
Return Some(RStruct)
if val
is a RStruct
, None
otherwise.
§Examples
use magnus::{define_global_const, eval, r_struct::define_struct, RStruct};
let struct_class = define_struct(None, ("foo", "bar")).unwrap();
define_global_const("Example", struct_class).unwrap();
assert!(RStruct::from_value(eval(r#"Example.new(1, 2)"#).unwrap()).is_some());
assert!(RStruct::from_value(eval(r#"Object.new"#).unwrap()).is_none());
sourcepub fn get<T>(self, index: usize) -> Result<T, Error>where
T: TryConvert,
pub fn get<T>(self, index: usize) -> Result<T, Error>where
T: TryConvert,
Return the value for the member at index
, where members are ordered
as per the member names when the struct class was defined.
§Examples
use magnus::{prelude::*, r_struct::define_struct, RStruct};
let struct_class = define_struct(None, ("foo", "bar")).unwrap();
let instance = RStruct::from_value(struct_class.new_instance((1, 2)).unwrap()).unwrap();
assert_eq!(instance.get::<i64>(0).unwrap(), 1);
assert_eq!(instance.get::<i64>(1).unwrap(), 2);
sourcepub fn aref<T, U>(self, index: T) -> Result<U, Error>where
T: IntoValue,
U: TryConvert,
pub fn aref<T, U>(self, index: T) -> Result<U, Error>where
T: IntoValue,
U: TryConvert,
Return the value for the member at index
.
index
may be an integer, string, or Symbol
.
§Examples
use magnus::{prelude::*, r_struct::define_struct, RStruct, Symbol};
let struct_class = define_struct(None, ("foo", "bar", "baz")).unwrap();
let instance = RStruct::from_value(struct_class.new_instance((1, 2, 3)).unwrap()).unwrap();
assert_eq!(instance.aref::<_, i64>(0).unwrap(), 1);
assert_eq!(instance.aref::<_, i64>("bar").unwrap(), 2);
assert_eq!(instance.aref::<_, i64>(Symbol::new("baz")).unwrap(), 3);
sourcepub fn aset<T, U>(self, index: T, val: U) -> Result<(), Error>
pub fn aset<T, U>(self, index: T, val: U) -> Result<(), Error>
Set the value for the member at index
.
index
may be an integer, string, or Symbol
.
§Examples
use magnus::{prelude::*, r_struct::define_struct, rb_assert, RStruct, Symbol};
let struct_class = define_struct(None, ("foo", "bar", "baz")).unwrap();
let instance = RStruct::from_value(struct_class.new_instance((1, 2, 3)).unwrap()).unwrap();
instance.aset(0, 4).unwrap();
rb_assert!("instance.foo == 4", instance);
instance.aset("bar", 5).unwrap();
rb_assert!("instance.bar == 5", instance);
instance.aset(Symbol::new("baz"), 6).unwrap();
rb_assert!("instance.baz == 6", instance);
sourcepub fn size(self) -> usize
pub fn size(self) -> usize
Returns the count of members this struct has.
§Examples
use magnus::{prelude::*, r_struct::define_struct, RStruct};
let struct_class = define_struct(None, ("foo", "bar", "baz")).unwrap();
let instance = RStruct::from_value(struct_class.new_instance(()).unwrap()).unwrap();
assert_eq!(instance.size(), 3);
sourcepub fn members(self) -> Result<Vec<Cow<'static, str>>, Error>
pub fn members(self) -> Result<Vec<Cow<'static, str>>, Error>
Returns the member names for this struct.
§Examples
use magnus::{prelude::*, r_struct::define_struct, RStruct};
let struct_class = define_struct(None, ("foo", "bar", "baz")).unwrap();
let instance = RStruct::from_value(struct_class.new_instance(()).unwrap()).unwrap();
assert_eq!(instance.members().unwrap(), &["foo", "bar", "baz"]);
sourcepub fn getmember<T, U>(self, id: T) -> Result<U, Error>where
T: IntoId,
U: TryConvert,
pub fn getmember<T, U>(self, id: T) -> Result<U, Error>where
T: IntoId,
U: TryConvert,
Return the value for the member named id
.
§Examples
use magnus::{prelude::*, r_struct::define_struct, RStruct};
let struct_class = define_struct(None, ("foo", "bar")).unwrap();
let instance = RStruct::from_value(struct_class.new_instance((1, 2)).unwrap()).unwrap();
assert_eq!(instance.getmember::<_, i64>("foo").unwrap(), 1);
assert_eq!(instance.getmember::<_, i64>("bar").unwrap(), 2);
Trait Implementations§
source§impl Object for RStruct
impl Object for RStruct
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 moresource§impl ReprValue for RStruct
impl ReprValue for RStruct
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,
Checks for equality, delegating to the Ruby method
#==
. 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,
Checks for equality, delegating to the Ruby method
#eql?
. Read moresource§fn hash(self) -> Result<Integer, Error>
fn hash(self) -> Result<Integer, Error>
Returns an integer non-uniquely identifying
self
. Read moresource§fn to_bool(self) -> bool
fn to_bool(self) -> bool
Convert
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,
Check if
self
responds to the given Ruby method. Read moresource§fn is_kind_of<T>(self, class: T) -> bool
fn is_kind_of<T>(self, class: T) -> bool
source§fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator ⓘwhere
M: IntoSymbol,
A: ArgList,
fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator ⓘwhere
M: IntoSymbol,
A: ArgList,
source§impl TryConvert for RStruct
impl TryConvert for RStruct
impl Copy for RStruct
Auto Trait Implementations§
impl Freeze for RStruct
impl RefUnwindSafe for RStruct
impl Send for RStruct
impl Sync for RStruct
impl Unpin for RStruct
impl UnwindSafe for RStruct
Blanket Implementations§
source§impl<T> AsRawValue for Twhere
T: ReprValue,
impl<T> AsRawValue for Twhere
T: ReprValue,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more