Sha256: 57bbfe7b1f20f2cd4fa16964c9c5e862b70a7af735b8cd7f2217064c1a3665db

Contents?: true

Size: 1.45 KB

Versions: 19

Compression:

Stored size: 1.45 KB

Contents

use std::cell::RefCell;

use magnus::{function, method, prelude::*, wrap};

struct Point {
    x: isize,
    y: isize,
}

#[wrap(class = "Point")]
struct MutPoint(RefCell<Point>);

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

    fn x(&self) -> isize {
        self.0.borrow().x
    }

    fn set_x(&self, val: isize) {
        self.0.borrow_mut().x = val;
    }

    fn y(&self) -> isize {
        self.0.borrow().y
    }

    fn set_y(&self, val: isize) {
        self.0.borrow_mut().y = val;
    }

    fn distance(&self, other: &MutPoint) -> f64 {
        (((other.x() - self.x()).pow(2) + (other.y() - self.y()).pow(2)) as f64).sqrt()
    }
}

fn main() -> Result<(), String> {
    magnus::Ruby::init(|ruby| {
        let class = ruby.define_class("Point", ruby.class_object())?;
        class.define_singleton_method("new", function!(MutPoint::new, 2))?;
        class.define_method("x", method!(MutPoint::x, 0))?;
        class.define_method("x=", method!(MutPoint::set_x, 1))?;
        class.define_method("y", method!(MutPoint::y, 0))?;
        class.define_method("y=", method!(MutPoint::set_y, 1))?;
        class.define_method("distance", method!(MutPoint::distance, 1))?;

        let d: f64 = ruby.eval(
            "a = Point.new(0, 0)
             b = Point.new(0, 0)
             b.x = 5
             b.y = 10
             a.distance(b)",
        )?;

        println!("{}", d);
        Ok(())
    })
}

Version data entries

19 entries across 19 versions & 1 rubygems

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