Struct RStruct

Source
pub struct RStruct(/* private fields */);
Expand description

A Value pointer to a RStruct struct, Ruby’s internal representation of ‘Structs’.

See the ReprValue and Object traits for additional methods available on this type.

Implementations§

Source§

impl RStruct

Source

pub fn from_value(val: Value) -> Option<Self>

Return Some(RStruct) if val is a RStruct, None otherwise.

§Examples
use magnus::{Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar"))?;
    ruby.define_global_const("Example", struct_class)?;

    assert!(RStruct::from_value(ruby.eval(r#"Example.new(1, 2)"#)?).is_some());
    assert!(RStruct::from_value(ruby.eval(r#"Object.new"#)?).is_none());

    Ok(())
}
Source

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::*, Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar"))?;
    let instance = RStruct::from_value(struct_class.new_instance((1, 2))?).unwrap();
    assert_eq!(instance.get::<i64>(0)?, 1);
    assert_eq!(instance.get::<i64>(1)?, 2);

    Ok(())
}
Source

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::*, Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar", "baz"))?;
    let instance = RStruct::from_value(struct_class.new_instance((1, 2, 3))?).unwrap();
    assert_eq!(instance.aref::<_, i64>(0)?, 1);
    assert_eq!(instance.aref::<_, i64>("bar")?, 2);
    assert_eq!(instance.aref::<_, i64>(ruby.to_symbol("baz"))?, 3);

    Ok(())
}
Source

pub fn aset<T, U>(self, index: T, val: U) -> Result<(), Error>
where T: IntoValue, U: IntoValue,

Set the value for the member at index.

index may be an integer, string, or Symbol.

§Examples
use magnus::{prelude::*, rb_assert, Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar", "baz"))?;
    let instance = RStruct::from_value(struct_class.new_instance((1, 2, 3))?).unwrap();

    instance.aset(0, 4)?;
    rb_assert!("instance.foo == 4", instance);

    instance.aset("bar", 5)?;
    rb_assert!("instance.bar == 5", instance);

    instance.aset(ruby.to_symbol("baz"), 6)?;
    rb_assert!("instance.baz == 6", instance);

    Ok(())
}
Source

pub fn size(self) -> usize

Returns the count of members this struct has.

§Examples
use magnus::{prelude::*, Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar", "baz"))?;
    let instance = RStruct::from_value(struct_class.new_instance(())?).unwrap();

    assert_eq!(instance.size(), 3);

    Ok(())
}
Source

pub fn members(self) -> Result<Vec<Cow<'static, str>>, Error>

Returns the member names for this struct.

§Examples
use magnus::{prelude::*, Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar", "baz"))?;
    let instance = RStruct::from_value(struct_class.new_instance(())?).unwrap();

    assert_eq!(instance.members()?, &["foo", "bar", "baz"]);

    Ok(())
}
Source

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::*, Error, RStruct, Ruby};

fn example(ruby: &Ruby) -> Result<(), Error> {
    let struct_class = ruby.define_struct(None, ("foo", "bar"))?;
    let instance = RStruct::from_value(struct_class.new_instance((1, 2))?).unwrap();
    assert_eq!(instance.getmember::<_, i64>("foo")?, 1);
    assert_eq!(instance.getmember::<_, i64>("bar")?, 2);

    Ok(())
}

Trait Implementations§

Source§

impl Clone for RStruct

Source§

fn clone(&self) -> RStruct

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RStruct

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for RStruct

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl IntoValue for RStruct

Source§

fn into_value_with(self, _: &Ruby) -> Value

Convert self into Value.
Source§

fn into_value(self) -> Value

Available on crate feature old-api only.
Convert self into Value. Read more
Source§

unsafe fn into_value_unchecked(self) -> Value

Convert self into Value. Read more
Source§

impl Object for RStruct

Source§

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
Source§

fn ivar_get<T, U>(self, name: T) -> Result<U, Error>
where T: IntoId, U: TryConvert,

Get the value for the instance variable name within self’s scope. Read more
Source§

fn ivar_set<T, U>(self, name: T, value: U) -> Result<(), Error>
where T: IntoId, U: IntoValue,

Set the value for the instance variable name within self’s scope. Read more
Source§

fn singleton_class(self) -> Result<RClass, Error>

Finds or creates the singleton class of self. Read more
Source§

fn extend_object(self, module: RModule) -> Result<(), Error>

Extend self with module. Read more
Source§

impl ReprValue for RStruct

Source§

fn as_value(self) -> Value

Return self as a Value.
Source§

fn is_nil(self) -> bool

Returns whether self is Ruby’s nil value. Read more
Source§

fn equal<T>(self, other: T) -> Result<bool, Error>
where T: ReprValue,

Checks for equality, delegating to the Ruby method #==. Read more
Source§

fn eql<T>(self, other: T) -> Result<bool, Error>
where T: ReprValue,

Checks for equality, delegating to the Ruby method #eql?. Read more
Source§

fn hash(self) -> Result<Integer, Error>

Returns an integer non-uniquely identifying self. Read more
Source§

fn class(self) -> RClass

Returns the class that self is an instance of. Read more
Source§

fn is_frozen(self) -> bool

Returns whether self is ‘frozen’. Read more
Source§

fn check_frozen(self) -> Result<(), Error>

Returns an error if self is ‘frozen’. Read more
Source§

fn freeze(self)

Mark self as frozen. Read more
Source§

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 more
Source§

fn funcall<M, A, T>(self, method: M, args: A) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the method named method on self with args. Read more
Source§

fn funcall_public<M, A, T>(self, method: M, args: A) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the public method named method on self with args. Read more
Source§

fn check_funcall<M, A, T>(self, method: M, args: A) -> Option<Result<T, Error>>
where M: IntoId, A: ArgList, T: TryConvert,

If self responds to the method named method, call it with args. Read more
Source§

fn funcall_with_block<M, A, T>( self, method: M, args: A, block: Proc, ) -> Result<T, Error>
where M: IntoId, A: ArgList, T: TryConvert,

Call the method named method on self with args and block. Read more
Source§

fn block_call<M, A, R, T>( self, method: M, args: A, block: fn(&Ruby, &[Value], Option<Proc>) -> R, ) -> Result<T, Error>
where M: IntoId, A: ArgList, R: BlockReturn, T: TryConvert,

Call the method named method on self with args and block. Read more
Source§

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 more
Source§

fn to_r_string(self) -> Result<RString, Error>

Convert self to a Ruby String. Read more
Source§

unsafe fn to_s(&self) -> Result<Cow<'_, str>, Error>

Convert self to a Rust string. Read more
Source§

fn inspect(self) -> String

Convert self to its Ruby debug representation. Read more
Source§

unsafe fn classname(&self) -> Cow<'_, str>

Return the name of self’s class. Read more
Source§

fn is_kind_of<T>(self, class: T) -> bool
where T: ReprValue + Module,

Returns whether or not self is an instance of class. Read more
Source§

fn enumeratorize<M, A>(self, method: M, args: A) -> Enumerator
where M: IntoSymbol, A: ArgList,

Generate an Enumerator from method on self, passing args to method. Read more
Source§

impl TryConvert for RStruct

Source§

fn try_convert(val: Value) -> Result<Self, Error>

Convert val into Self.
Source§

impl Copy for RStruct

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsRawValue for T
where T: ReprValue,

Source§

fn as_raw(self) -> u64

Available on crate feature rb-sys only.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Inspect for T
where T: Debug,

Source§

fn inspect(&self) -> String

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> BlockReturn for T
where T: BlockReturn,

Source§

impl<T> Locate for T
where T: ReprValue,

Source§

impl<T> Mark for T
where T: ReprValue,

Source§

impl<T> ReturnValue for T
where T: ReturnValue,