Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ergonomic struct API for Rust #329

Open
Wodann opened this issue May 8, 2021 · 0 comments
Open

Ergonomic struct API for Rust #329

Wodann opened this issue May 8, 2021 · 0 comments
Labels
exp: high Achievable by investing significant time and effort, potentially through collaboration pri: low An issue with no impact to quality, performance, or functionality type: feat New feature or request

Comments

@Wodann
Copy link
Collaborator

Wodann commented May 8, 2021

Currently, it looks like this when marshalling Mun structs to Rust:

let lib_path = env::args().nth(1).expect("Expected path to a Mun library.");

let mut runtime = 
  RuntimeBuilder::new(lib_path)
    .spawn()
    .expect("Failed to spawn Runtime");

  let runtime_ref = runtime.borrow();
  let mut xy: StructRef = invoke_fn!(runtime_ref, "vector2_new", -1.0f32, 1.0f32).unwrap();
  let x: f32 = xy.get("x").unwrap();
  xy.set("x", x * x).unwrap();
  let y = xy.replace("y", -1.0f32).unwrap();
}

We want to make these actions more ergonomic, safe and (potentially) faster by creating strongly typed Rust structs, e.g.:

#[derive(Mun)]
struct Vector2 {
  x: f32,
  y: f32,
}

This would allow us to simplify the previous code sample, e.g. as follows:

let lib_path = env::args().nth(1).expect("Expected path to a Mun library.");

let mut runtime = 
  RuntimeBuilder::new(lib_path)
    .spawn()
    .expect("Failed to spawn Runtime");

  let runtime_ref = runtime.borrow();
  let mut xy: StructRef = invoke_fn!(runtime_ref, "vector2_new", -1.0f32, 1.0f32).unwrap();
  let x: f32 = xy.x();
  xy.set_x(x * x);
  let y = xy.replace_y(-1.0f32);
}

Apart from making programming with Mun simpler, this would allow us to auto-generate functions for verifying the validity of a struct's fields upon Runtime start-up instead of always needing to check validity when you marshal a field.

@Wodann Wodann added type: perf Changes that improve performance pri: low An issue with no impact to quality, performance, or functionality exp: high Achievable by investing significant time and effort, potentially through collaboration labels May 8, 2021
@Wodann Wodann added type: feat New feature or request and removed type: perf Changes that improve performance labels Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exp: high Achievable by investing significant time and effort, potentially through collaboration pri: low An issue with no impact to quality, performance, or functionality type: feat New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant