Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added a ToLua derive macro #427

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

Open
ArchangelJTW wants to merge 17 commits into mlua-rs:main
base: main
Choose a base branch
Loading
from ArchangelJTW:to_lua_macro

Conversation

@ArchangelJTW
Copy link

@ArchangelJTW ArchangelJTW commented Jul 12, 2024
edited
Loading

I just made it but it seems useful so far, at least for me it is, works with most primitives so I think it's worth adding to help reduce boilerplate:

use mlua::{FromLua, Function, Lua, LuaOptions, StdLib, ToLua};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, FromLua, Clone, ToLua)] // new ToLua derive macro
struct MyType {
 x: u32,
 y: String,
}
fn main() {
 let lua = Lua::new_with(StdLib::ALL_SAFE, LuaOptions::default()).unwrap();
 
 let lua_code = r#"
 function(myType)
 print(myType.x, myType.y)
 end
 "#;
 let f: Function = lua.load(lua_code).eval().unwrap();
 let my_type = MyType {
 x: 6,
 y: "hi, from rust to lua".into()
 };
 f.call::<MyType, ()>(my_type).unwrap();
}

alerque, PatWie, asudox, and ElhamAryanpur reacted with hooray emoji CM-IV, alerque, asudox, and ElhamAryanpur reacted with eyes emoji
Copy link
Author

ArchangelJTW commented Jul 13, 2024
edited
Loading

I updated it to implement UserData instead of IntoLua, this works more cleanly with passing the type in and out of lua, it will use copy on rust primitives, otherwise it will clone. It could probably be updated to include field annotations of some kind in order to be more customizable and efficient. It currently creates a simple getter and setter for all the struct fields.

Copy link
Author

Added a ToLuaTable and FromLuaTable macro, this essentially, in a way, allows you serialize and deserialize Lua tables to and from rust types.

Copy link
Contributor

vhyrro commented Nov 13, 2024

This would be a fantastic addition, are there any major blockers that must be addressed for this PR? I volunteer to help out if need be :)

asudox reacted with thumbs up emoji daltongd reacted with rocket emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /