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" license = "MIT"
[dependencies] [dependencies]
rlua = "0.15" rlua = "0.16"
serde = "1.0" serde = "1.0"
[dev-dependencies] [dev-dependencies]

View File

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

View File

@ -20,20 +20,20 @@
//! } //! }
//! //!
//! let lua = rlua::Lua::new(); //! let lua = rlua::Lua::new();
//! let foo = Foo { //! lua.context(|lua| {
//! bar: 42, //! let foo = Foo {
//! baz: vec![String::from("fizz"), String::from("buzz")], //! bar: 42,
//! }; //! baz: vec![String::from("fizz"), String::from("buzz")],
//! };
//! //!
//! let value = rlua_serde::to_value(&lua, &foo).unwrap(); //! let value = rlua_serde::to_value(lua, &foo).unwrap();
//! lua.globals().set("value", value).unwrap(); //! lua.globals().set("value", value).unwrap();
//! lua.exec::<_, ()>( //! lua.load(
//! r#" //! r#"
//! assert(value["bar"] == 42) //! assert(value["bar"] == 42)
//! assert(value["baz"][2] == "buzz") //! assert(value["baz"][2] == "buzz")
//! "#, //! "#).exec().unwrap();
//! None, //! });
//! ).unwrap()
//! } //! }
//! ``` //! ```
@ -49,10 +49,10 @@ pub mod ser;
pub mod de; 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 }; let serializer = ser::Serializer { lua };
Ok(t.serialize(serializer)?) Ok(t.serialize(serializer)?)
} }

View File

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