Sha256: 3310ecfb45f39cbc10a70656c647d6bac39375554365a6ae42e12a7056db601f

Contents?: true

Size: 1.75 KB

Versions: 19

Compression:

Stored size: 1.75 KB

Contents

use magnus::{
    embed::init, function, gc, method, prelude::*, typed_data::Obj, value::Opaque,
    DataTypeFunctions, TypedData,
};

#[magnus::wrap(class = "Point", free_immediately)]
struct Point {
    x: isize,
    y: isize,
}

impl Point {
    fn new(x: isize, y: isize) -> Self {
        Self { x, y }
    }
}

#[derive(TypedData)]
#[magnus(class = "Line", free_immediately, mark)]
struct Line {
    #[magnus(opaque_attr_reader)]
    start: Opaque<Obj<Point>>,
    #[magnus(opaque_attr_reader)]
    end: Opaque<Obj<Point>>,
}

impl Line {
    fn new(start: Obj<Point>, end: Obj<Point>) -> Self {
        Self {
            start: start.into(),
            end: end.into(),
        }
    }

    fn length(&self) -> f64 {
        let start = self.start();
        let end = self.end();

        (((end.x - start.x).pow(2) + (end.y - start.y).pow(2)) as f64).sqrt()
    }
}

impl DataTypeFunctions for Line {
    fn mark(&self, marker: &gc::Marker) {
        marker.mark(self.start);
        marker.mark(self.end);
    }
}

#[test]
fn it_can_nest_wrapped_structs() {
    let ruby = unsafe { init() };

    let class = ruby.define_class("Point", ruby.class_object()).unwrap();
    class
        .define_singleton_method("new", function!(Point::new, 2))
        .unwrap();

    let class = ruby.define_class("Line", ruby.class_object()).unwrap();
    class
        .define_singleton_method("new", function!(Line::new, 2))
        .unwrap();
    class
        .define_method("length", method!(Line::length, 0))
        .unwrap();

    let result: f64 = ruby
        .eval(
            r#"
        start = Point.new(0, 0)
        finish = Point.new(10, 10)
        line = Line.new(start, finish)
        line.length
    "#,
        )
        .unwrap();

    assert!(result - 14.14213 < 0.00001);
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/typed_data_obj.rs
wasmtime-28.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/typed_data_obj.rs
wasmtime-27.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/typed_data_obj.rs
wasmtime-26.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/typed_data_obj.rs
wasmtime-25.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-25.0.1 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-25.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-24.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-23.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-22.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-21.0.1 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-20.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-20.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/typed_data_obj.rs
wasmtime-18.0.3 ./ext/cargo-vendor/magnus-0.6.2/tests/typed_data_obj.rs
wasmtime-17.0.1 ./ext/cargo-vendor/magnus-0.6.2/tests/typed_data_obj.rs
wasmtime-17.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/typed_data_obj.rs
wasmtime-16.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/typed_data_obj.rs
wasmtime-15.0.1 ./ext/cargo-vendor/magnus-0.6.2/tests/typed_data_obj.rs
wasmtime-15.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/typed_data_obj.rs