Update tests, add example usage

This commit is contained in:
zrkn 2018-10-11 16:05:27 +03:00
parent 456eac9a56
commit 52c43e3413
3 changed files with 49 additions and 10 deletions

View File

@ -259,7 +259,7 @@ mod tests {
let expected = Test { int: 1, seq: vec!["a".to_owned(), "b".to_owned()] };
let lua = Lua::new();
let value = lua.exec::<Value>(
let value = lua.exec::<_, Value>(
r#"
a = {}
a.int = 1
@ -286,7 +286,7 @@ mod tests {
let lua = Lua::new();
let expected = E::Unit;
let value = lua.exec::<Value>(
let value = lua.exec::<_, Value>(
r#"
return "Unit"
"#,
@ -297,7 +297,7 @@ mod tests {
let expected = E::Newtype(1);
let value = lua.exec::<Value>(
let value = lua.exec::<_, Value>(
r#"
a = {}
a["Newtype"] = 1
@ -309,7 +309,7 @@ mod tests {
assert_eq!(expected, got);
let expected = E::Tuple(1, 2);
let value = lua.exec::<Value>(
let value = lua.exec::<_, Value>(
r#"
a = {}
a["Tuple"] = {1, 2}
@ -321,7 +321,7 @@ mod tests {
assert_eq!(expected, got);
let expected = E::Struct { a: 1 };
let value = lua.exec::<Value>(
let value = lua.exec::<_, Value>(
r#"
a = {}
a["Struct"] = {}

View File

@ -1,3 +1,42 @@
//! This crate allows you to serialize and deserialize any type that implements
//! `serde::Serialize` and `serde::Deserialzie` into/from `rlua::Value`.
//!
//! Implementation is similar to `serde_json::Value`
//!
//! Example usage:
//!
//! ```rust
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//! extern crate rlua;
//! extern crate rlua_serde;
//!
//! fn main() {
//! #[derive(Serialize, Deserialize)]
//! struct Foo {
//! bar: u32,
//! baz: Vec<String>,
//! }
//!
//! let lua = rlua::Lua::new();
//! let foo = Foo {
//! bar: 42,
//! baz: vec![String::from("fizz"), String::from("buzz")],
//! };
//!
//! let value = rlua_serde::to_value(&lua, &foo).unwrap();
//! lua.globals().set("value", value).unwrap();
//! lua.exec::<_, ()>(
//! r#"
//! assert(value["bar"] == 42)
//! assert(value["baz"][2] == "buzz")
//! "#,
//! None,
//! ).unwrap()
//! }
//! ```
extern crate rlua;
#[macro_use]
extern crate serde;

View File

@ -380,7 +380,7 @@ mod tests {
let lua = Lua::new();
let value = to_value(&lua, &test).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<()>(
lua.exec::<_, ()>(
r#"
assert(value["int"] == 1)
assert(value["seq"][1] == "a")
@ -405,7 +405,7 @@ mod tests {
let u = E::Unit;
let value = to_value(&lua, &u).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<()>(
lua.exec::<_, ()>(
r#"
assert(value == "Unit")
"#,
@ -415,7 +415,7 @@ mod tests {
let n = E::Newtype(1);
let value = to_value(&lua, &n).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<()>(
lua.exec::<_, ()>(
r#"
assert(value["Newtype"] == 1)
"#,
@ -425,7 +425,7 @@ mod tests {
let t = E::Tuple(1, 2);
let value = to_value(&lua, &t).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<()>(
lua.exec::<_, ()>(
r#"
assert(value["Tuple"][1] == 1)
assert(value["Tuple"][2] == 2)
@ -436,7 +436,7 @@ mod tests {
let s = E::Struct { a: 1 };
let value = to_value(&lua, &s).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<()>(
lua.exec::<_, ()>(
r#"
assert(value["Struct"]["a"] == 1)
"#,