extern_methods!() { /* proc-macro */ }
Expand description

Define methods for DataPtr.

Examples

extern crate minutus;

#[minutus::wrap(class_method = "new", method = "distance")]
struct Point {
    x: i64,
    y: i64,
}

impl Point {
    #[minutus::class_method]
    pub fn new(x: i64, y: i64) -> Self {
        Self { x, y }
    }

    #[minutus::method]
    pub fn distance(&self, other: &Point) -> f64 {
        (((self.x - other.x).pow(2) + (self.y - other.y).pow(2)) as f64).sqrt()
    }
}

minutus::extern_methods! {
    Point;
    fn name() -> String; // class method
    fn inspect(self) -> String; // instance method
}

fn main() {
    use minutus::types::TryFromMrb; // for using `Point::try_from_mrb`

    let runtime = minutus::Evaluator::build();
    Point::define_class_on_mrb(runtime.mrb());

    let point = Point::try_from_mrb(runtime.evaluate("Point.new(1,2)").unwrap()).unwrap();
    // evaluates `point.inspect` in mruby world, and returns its value
    point.inspect(); // => "#<Point:0x140009fb0>"

    // evaluates `Point.name` in mruby world, and returns its value
    // note: class methods requires `mrb` as argument
    Point::name(runtime.mrb()); // => "Point"
}