Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 194 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ derive = ["dep:mlua-extras-derive"]
[dev-dependencies]
serde = { version = "1.0.228", features = ["derive"] }
tempfile = "3"
tokio = { version = "1", features = ["macros", "rt"] }

[dependencies]
mlua-extras-derive = { path = "./mlua_extras_derive", version = "11.6.0", optional = true }

mlua = { version = "0.11.6", optional = true, default-features = false }
strum = { version = "0.27.2", features = ["derive"], default-features = false }

[[example]]
name = "macros"
required-features = ["mlua", "derive"]

[[example]]
name = "enum_and_tuple"
required-features = ["mlua"]
Expand Down
37 changes: 29 additions & 8 deletions examples/enum_and_tuple.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use mlua::MetaMethod;
use mlua::{MetaMethod, UserData};
use mlua_extras::{
Typed, UserData, mlua,
Typed, mlua,
typed::{
Type, TypedDataFields, TypedDataMethods, TypedUserData,
Type, TypedDataFields, TypedDataMethods, TypedUserData, WrappedBuilder,
generator::{Definition, DefinitionFileGenerator, Definitions},
},
};
use std::path::PathBuf;

#[derive(Typed, UserData)]
#[derive(Typed)]
enum Kind {
A(String),
B { name: String, data: String },
B {
name: String,
data: String,
},
#[allow(unused)]
C,
}

Expand All @@ -25,6 +29,18 @@ impl Kind {
}
}

impl UserData for Kind {
fn add_fields<F: mlua::UserDataFields<Self>>(fields: &mut F) {
let mut wrapper = WrappedBuilder::new(fields);
<Self as TypedUserData>::add_fields(&mut wrapper);
}

fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
let mut wrapper = mlua_extras::typed::WrappedBuilder::new(methods);
<Self as TypedUserData>::add_methods(&mut wrapper);
}
}

impl TypedUserData for Kind {
fn add_fields<T: TypedDataFields<Self>>(fields: &mut T) {
fields.add_field_method_get("name", |_lua, this: &Self| match this {
Expand All @@ -45,7 +61,7 @@ impl TypedUserData for Kind {
});

fields
.coerce(Type::named("KindEnum"))
.coerce(Type::named("KindVariant"))
.add_field_function_get("_variant", |_lua, this| {
match *this.borrow::<Self>().unwrap() {
Self::A(_) => Ok("A"),
Expand Down Expand Up @@ -97,7 +113,7 @@ fn main() -> mlua::Result<()> {
.load(
r#"
print(KindA[1])
print(KindA["1"])
-- print(KindA["1"])
print(KindB.name)
print(KindB.data)

Expand All @@ -120,7 +136,12 @@ fn main() -> mlua::Result<()> {
}

let definitions: Definitions = Definitions::start()
.define("enum_and_tuple", Definition::start().register::<Kind>("Kind"))
.define(
"enum_and_tuple",
Definition::start()
.register::<Kind>("Kind")
.register_as("KindVariant", Type::literal("A") | "B" | "C"),
)
.finish();

let types_path = PathBuf::from("examples/types");
Expand Down
Loading