Thanks to visit codestin.com
Credit goes to docs.rs

Inspect

Trait Inspect 

Source
pub trait Inspect {
    // Required method
    fn inspect(&self) -> String;
}
Expand description

Trait for a Ruby-compatible #inspect method.

Automatically implemented for any type implementing Debug.

See also Dup, IsEql, typed_data::Cmp, and typed_data::Hash.

§Examples

use std::fmt;

use magnus::{
    function, gc, method, prelude::*, rb_assert, typed_data, value::Opaque, DataTypeFunctions,
    Error, Ruby, TypedData, Value,
};

#[derive(TypedData)]
#[magnus(class = "Pair", free_immediately, mark)]
struct Pair {
    #[magnus(opaque_attr_reader)]
    a: Opaque<Value>,
    #[magnus(opaque_attr_reader)]
    b: Opaque<Value>,
}

impl Pair {
    fn new(a: Value, b: Value) -> Self {
        Self {
            a: a.into(),
            b: b.into(),
        }
    }
}

impl DataTypeFunctions for Pair {
    fn mark(&self, marker: &gc::Marker) {
        marker.mark(self.a);
        marker.mark(self.b);
    }
}

impl fmt::Debug for Pair {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Pair")
            .field("a", &self.a())
            .field("b", &self.b())
            .finish()
    }
}

fn example(ruby: &Ruby) -> Result<(), Error> {
    let class = ruby.define_class("Pair", ruby.class_object())?;
    class.define_singleton_method("new", function!(Pair::new, 2))?;
    class.define_method(
        "inspect",
        method!(<Pair as typed_data::Inspect>::inspect, 0),
    )?;

    let pair = Pair::new(
        ruby.str_new("foo").as_value(),
        ruby.integer_from_i64(1).as_value(),
    );
    rb_assert!(ruby, r#"pair.inspect == "Pair { a: \"foo\", b: 1 }""#, pair);

    Ok(())
}

Required Methods§

Source

fn inspect(&self) -> String

Implementors§

Source§

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