Merge pull request #5 from dariusc93/master

Update rlua to 0.16
This commit is contained in:
zrkn 2019-02-08 14:34:55 +03:00 committed by GitHub
commit fb4b17b3f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 101 deletions

View File

@ -9,7 +9,7 @@ keywords = ["lua", "serde"]
license = "MIT"
[dependencies]
rlua = "0.15"
rlua = "0.16"
serde = "1.0"
[dev-dependencies]

View File

@ -259,17 +259,16 @@ mod tests {
let expected = Test { int: 1, seq: vec!["a".to_owned(), "b".to_owned()] };
let lua = Lua::new();
let value = lua.exec::<_, Value>(
r#"
lua.context(|lua| {
let value = lua.load(r#"
a = {}
a.int = 1
a.seq = {"a", "b"}
return a
"#,
None,
).unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
"#).eval().unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
});
}
@ -284,53 +283,46 @@ mod tests {
}
let lua = Lua::new();
let expected = E::Unit;
let value = lua.exec::<_, Value>(
r#"
lua.context(|lua| {
let expected = E::Unit;
let value = lua.load(
r#"
return "Unit"
"#,
None,
).unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
"#).eval().unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = E::Newtype(1);
let value = lua.exec::<_, Value>(
r#"
let expected = E::Newtype(1);
let value = lua.load(
r#"
a = {}
a["Newtype"] = 1
return a
"#,
None,
).unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
"#).eval().unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = E::Tuple(1, 2);
let value = lua.exec::<_, Value>(
r#"
let expected = E::Tuple(1, 2);
let value = lua.load(
r#"
a = {}
a["Tuple"] = {1, 2}
return a
"#,
None,
).unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
"#).eval().unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
let expected = E::Struct { a: 1 };
let value = lua.exec::<_, Value>(
r#"
let expected = E::Struct { a: 1 };
let value = lua.load(
r#"
a = {}
a["Struct"] = {}
a["Struct"]["a"] = 1
return a
"#,
None,
).unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
"#).eval().unwrap();
let got = from_value(value).unwrap();
assert_eq!(expected, got);
});
}
}

View File

@ -20,20 +20,20 @@
//! }
//!
//! let lua = rlua::Lua::new();
//! let foo = Foo {
//! bar: 42,
//! baz: vec![String::from("fizz"), String::from("buzz")],
//! };
//! lua.context(|lua| {
//! 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()
//! let value = rlua_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();
//! });
//! }
//! ```
@ -49,10 +49,10 @@ pub mod ser;
pub mod de;
use rlua::{Lua, Value, Error};
use rlua::{Context, Value, Error};
pub fn to_value<T: serde::Serialize>(lua: &Lua, t: T) -> Result<Value, Error> {
pub fn to_value<'lua, T: serde::Serialize>(lua: Context<'lua>, t: T) -> Result<Value<'lua>, Error> {
let serializer = ser::Serializer { lua };
Ok(t.serialize(serializer)?)
}

View File

@ -1,13 +1,13 @@
use serde;
use rlua::{Lua, Value, Table, String as LuaString};
use rlua::{Context, Lua, Value, Table, String as LuaString};
use to_value;
use error::{Error, Result};
pub struct Serializer<'lua> {
pub lua: &'lua Lua,
pub lua: Context<'lua>,
}
impl<'lua> serde::Serializer for Serializer<'lua> {
@ -208,7 +208,7 @@ impl<'lua> serde::Serializer for Serializer<'lua> {
pub struct SerializeVec<'lua> {
lua: &'lua Lua,
lua: Context<'lua>,
table: Table<'lua>,
idx: u64,
}
@ -262,7 +262,7 @@ impl<'lua> serde::ser::SerializeTupleStruct for SerializeVec<'lua> {
pub struct SerializeTupleVariant<'lua> {
lua: &'lua Lua,
lua: Context<'lua>,
name: LuaString<'lua>,
table: Table<'lua>,
idx: u64,
@ -289,7 +289,7 @@ impl<'lua> serde::ser::SerializeTupleVariant for SerializeTupleVariant<'lua> {
pub struct SerializeMap<'lua> {
lua: &'lua Lua,
lua: Context<'lua>,
table: Table<'lua>,
next_key: Option<Value<'lua>>
}
@ -339,7 +339,7 @@ impl<'lua> serde::ser::SerializeStruct for SerializeMap<'lua> {
pub struct SerializeStructVariant<'lua> {
lua: &'lua Lua,
lua: Context<'lua>,
name: LuaString<'lua>,
table: Table<'lua>,
}
@ -378,16 +378,16 @@ mod tests {
let test = Test { int: 1, seq: vec!["a", "b"] };
let lua = Lua::new();
let value = to_value(&lua, &test).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<_, ()>(
r#"
lua.context(|lua| {
let value = to_value(lua, &test).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(
r#"
assert(value["int"] == 1)
assert(value["seq"][1] == "a")
assert(value["seq"][2] == "b")
"#,
None,
).unwrap();
"#).exec()
}).unwrap()
}
#[test]
@ -402,45 +402,35 @@ mod tests {
let lua = Lua::new();
let u = E::Unit;
let value = to_value(&lua, &u).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<_, ()>(
r#"
lua.context(|lua| {
let u = E::Unit;
let value = to_value(lua, &u).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
assert(value == "Unit")
"#,
None,
).unwrap();
"#).exec().unwrap();
let n = E::Newtype(1);
let value = to_value(&lua, &n).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<_, ()>(
r#"
let n = E::Newtype(1);
let value = to_value(lua, &n).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
assert(value["Newtype"] == 1)
"#,
None,
).unwrap();
"#).exec().unwrap();
let t = E::Tuple(1, 2);
let value = to_value(&lua, &t).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<_, ()>(
r#"
let t = E::Tuple(1, 2);
let value = to_value(lua, &t).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
assert(value["Tuple"][1] == 1)
assert(value["Tuple"][2] == 2)
"#,
None,
).unwrap();
"#).exec().unwrap();
let s = E::Struct { a: 1 };
let value = to_value(&lua, &s).unwrap();
lua.globals().set("value", value).unwrap();
lua.exec::<_, ()>(
r#"
let s = E::Struct { a: 1 };
let value = to_value(lua, &s).unwrap();
lua.globals().set("value", value).unwrap();
lua.load(r#"
assert(value["Struct"]["a"] == 1)
"#,
None,
).unwrap();
"#).exec()
}).unwrap();
}
}