2019-04-26 10:24:48 +00:00
|
|
|
# rlue_serde
|
2018-02-13 12:12:22 +00:00
|
|
|
|
2020-07-06 00:58:23 +00:00
|
|
|
Implementation of [serde](https://serde.rs/) Serializer/Deserializer for
|
|
|
|
[mlua::Value](https://docs.rs/mlua/0.4.1/mlua/enum.Value.html)
|
|
|
|
|
2019-04-26 10:24:48 +00:00
|
|
|
|
|
|
|
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
|
2020-07-06 00:57:24 +00:00
|
|
|
[![Crates.io](https://img.shields.io/crates/v/mlua_serde.svg)](https://crates.io/crates/mlua_serde)
|
|
|
|
[![Documentation](https://docs.rs/mlua_serde/badge.svg)][dox]
|
2019-04-26 10:24:48 +00:00
|
|
|
|
|
|
|
More information about this crate can be found in the [crate documentation][dox].
|
|
|
|
|
2020-07-06 00:57:24 +00:00
|
|
|
[dox]: https://docs.rs/mlua_serde/*/mlua_serde/
|
2019-04-26 10:24:48 +00:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2020-07-06 00:57:24 +00:00
|
|
|
To use `mlua_serde`, first add this to your `Cargo.toml`:
|
2019-04-26 10:24:48 +00:00
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
2020-07-06 01:07:38 +00:00
|
|
|
mlua_serde = { version = "0.5", features = ["lua53"] }
|
|
|
|
```
|
|
|
|
|
|
|
|
You must choose between Lua 5.3 and Lua 5.4. To use Lua 5.4, change `lua53` for
|
|
|
|
`lua54` like this:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
mlua_serde = { version = "0.5", features = ["lua54"] }
|
|
|
|
```
|
|
|
|
|
|
|
|
If you don't have lua installed on your system for some reason, use the `vendored`
|
|
|
|
feature:
|
|
|
|
|
|
|
|
```toml
|
|
|
|
[dependencies]
|
|
|
|
mlua_serde = { version = "0.5", features = ["lua53", "vendored"] }
|
2019-04-26 10:24:48 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Next, you can use `to_value`/`from_value` functions to serialize/deserialize:
|
|
|
|
|
|
|
|
```rust
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
struct Foo {
|
|
|
|
bar: u32,
|
|
|
|
baz: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-07-06 00:57:24 +00:00
|
|
|
let lua = mlua::Lua::new();
|
|
|
|
let foo = Foo {
|
|
|
|
bar: 42,
|
|
|
|
baz: vec![String::from("fizz"), String::from("buzz")],
|
|
|
|
};
|
|
|
|
|
|
|
|
let value = mlua_serde::to_value(lua, &foo).unwrap();
|
|
|
|
lua.globals().set("value", value).unwrap();
|
|
|
|
lua.load(
|
|
|
|
r#"
|
|
|
|
assert(value["bar"] == 42)
|
|
|
|
assert(value["baz"][2] == "buzz")
|
|
|
|
"#).exec().unwrap();
|
2019-04-26 10:24:48 +00:00
|
|
|
}
|
|
|
|
```
|