From 96b8bd0eb3da435e36a71d29939c32e55dfff170 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 13:12:22 +0300 Subject: [PATCH 01/11] WIP: Serde support --- Cargo.toml | 8 ++ src/lib.rs | 4 + src/wrapper/mod.rs | 2 + src/wrapper/serde.rs | 335 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 349 insertions(+) create mode 100644 src/wrapper/serde.rs diff --git a/Cargo.toml b/Cargo.toml index b2c31fe..67dc216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,4 +20,12 @@ gcc = "0.3" [dependencies] bitflags = "0.1" libc = "0.2.13" +serde = { version="1.0.0", optional=true } +quick-error = "1.2.0" +[dev-dependencies] +serde_json = "1.0.0" + +[features] +default = ["serialize"] +serialize = ["serde"] diff --git a/src/lib.rs b/src/lib.rs index f3db8fd..cca0d22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,10 @@ pub extern crate libc; #[macro_use] extern crate bitflags; +#[macro_use] +extern crate quick_error; +#[cfg(feature="serialize")] +extern crate serde; pub use wrapper::state::{ State, diff --git a/src/wrapper/mod.rs b/src/wrapper/mod.rs index 1fbaee8..28e1b12 100644 --- a/src/wrapper/mod.rs +++ b/src/wrapper/mod.rs @@ -24,4 +24,6 @@ pub mod convert; pub mod state; +#[cfg(feature="serialize")] +mod serde; diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs new file mode 100644 index 0000000..22c2a84 --- /dev/null +++ b/src/wrapper/serde.rs @@ -0,0 +1,335 @@ +use std::fmt; +use serde::{Serialize, Serializer, ser}; + +use wrapper::convert::ToLua; +use wrapper::state::State; + +pub struct Serde<'a, S: Serialize + ?Sized + 'a>(&'a S); + +struct LuaSerializer<'a>(&'a mut State); + +struct SerializeSeq<'a>(&'a mut State); +struct SerializeTuple<'a>(&'a mut State); +struct SerializeTupleStruct<'a>(&'a mut State); +struct SerializeTupleVariant<'a>(&'a mut State); +struct SerializeMap<'a>(&'a mut State); +struct SerializeStruct<'a>(&'a mut State); +struct SerializeStructVariant<'a>(&'a mut State); + + +quick_error! { + #[derive(Debug)] + pub enum Error wraps ErrorEnum { + Custom(msg: String) { + display("custom serialization error: {}", msg) + description("custom serialization error") + } + } +} + +impl ser::Error for Error { + fn custom(msg: T) -> Self { + ErrorEnum::Custom(msg.to_string()).into() + } +} + + +impl<'a> ser::SerializeSeq for SerializeSeq<'a> { + type Ok = (); + type Error = Error; + fn serialize_element( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> ser::SerializeTuple for SerializeTuple<'a> { + type Ok = (); + type Error = Error; + fn serialize_element( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> ser::SerializeTupleStruct for SerializeTupleStruct<'a> { + type Ok = (); + type Error = Error; + fn serialize_field( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> ser::SerializeTupleVariant for SerializeTupleVariant<'a> { + type Ok = (); + type Error = Error; + fn serialize_field( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> ser::SerializeMap for SerializeMap<'a> { + type Ok = (); + type Error = Error; + fn serialize_key(&mut self, key: &T) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn serialize_value( + &mut self, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> ser::SerializeStruct for SerializeStruct<'a> { + type Ok = (); + type Error = Error; + fn serialize_field( + &mut self, + key: &'static str, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> ser::SerializeStructVariant for SerializeStructVariant<'a> { + type Ok = (); + type Error = Error; + fn serialize_field( + &mut self, + key: &'static str, + value: &T + ) -> Result<(), Self::Error> + where + T: Serialize + { + unimplemented!(); + } + fn end(self) -> Result { + unimplemented!(); + } +} + +impl<'a> Serializer for LuaSerializer<'a> { + type Ok = (); + type Error = Error; + type SerializeSeq = SerializeSeq<'a>; + type SerializeTuple = SerializeTuple<'a>; + type SerializeTupleStruct = SerializeTupleStruct<'a>; + type SerializeTupleVariant = SerializeTupleVariant<'a>; + type SerializeMap = SerializeMap<'a>; + type SerializeStruct = SerializeStruct<'a>; + type SerializeStructVariant = SerializeStructVariant<'a>; + fn serialize_bool(self, v: bool) -> Result { + unimplemented!(); + } + fn serialize_i8(self, v: i8) -> Result { + unimplemented!(); + } + fn serialize_i16(self, v: i16) -> Result { + unimplemented!(); + } + fn serialize_i32(self, v: i32) -> Result { + unimplemented!(); + } + fn serialize_i64(self, v: i64) -> Result { + unimplemented!(); + } + fn serialize_u8(self, v: u8) -> Result { + unimplemented!(); + } + fn serialize_u16(self, v: u16) -> Result { + unimplemented!(); + } + fn serialize_u32(self, v: u32) -> Result { + unimplemented!(); + } + fn serialize_u64(self, v: u64) -> Result { + unimplemented!(); + } + fn serialize_f32(self, v: f32) -> Result { + unimplemented!(); + } + fn serialize_f64(self, v: f64) -> Result { + unimplemented!(); + } + fn serialize_char(self, v: char) -> Result { + unimplemented!(); + } + fn serialize_str(self, v: &str) -> Result { + self.0.push_string(v); + Ok(()) + } + fn serialize_bytes(self, v: &[u8]) -> Result { + unimplemented!(); + } + fn serialize_none(self) -> Result { + unimplemented!(); + } + fn serialize_some( + self, + value: &T + ) -> Result + where + T: Serialize + { + unimplemented!(); + } + fn serialize_unit(self) -> Result { + unimplemented!(); + } + fn serialize_unit_struct( + self, + name: &'static str + ) -> Result { + unimplemented!(); + } + fn serialize_unit_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str + ) -> Result { + unimplemented!(); + } + fn serialize_newtype_struct( + self, + name: &'static str, + value: &T + ) -> Result + where + T: Serialize { + unimplemented!(); + } + fn serialize_newtype_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + value: &T + ) -> Result + where + T: Serialize { + unimplemented!(); + } + fn serialize_seq( + self, + len: Option + ) -> Result { + unimplemented!(); + } + fn serialize_tuple( + self, + len: usize + ) -> Result { + unimplemented!(); + } + fn serialize_tuple_struct( + self, + name: &'static str, + len: usize + ) -> Result { + unimplemented!(); + } + fn serialize_tuple_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize + ) -> Result { + unimplemented!(); + } + fn serialize_map( + self, + len: Option + ) -> Result { + unimplemented!(); + } + fn serialize_struct( + self, + name: &'static str, + len: usize + ) -> Result { + unimplemented!(); + } + fn serialize_struct_variant( + self, + name: &'static str, + variant_index: u32, + variant: &'static str, + len: usize + ) -> Result { + unimplemented!(); + } +} + +impl<'a, T: Serialize + 'a> ToLua for Serde<'a, T> { + fn to_lua(&self, state: &mut State) { + self.0.serialize(LuaSerializer(state)) + .expect("serialization error") + } +} + +#[cfg(test)] +mod test { + use {State}; + use super::Serde; + + #[test] + fn serialize_str() { + let mut state = State::new(); + state.push(Serde(&"hello")); + } +} From 53844a53bc9c52ef9693d5d47ea10bd5afd35c9f Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 16:11:05 +0300 Subject: [PATCH 02/11] Implements serialization for integers and floats --- src/wrapper/serde.rs | 66 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 22c2a84..6125699 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -1,4 +1,5 @@ use std::fmt; +use std::i64; use serde::{Serialize, Serializer, ser}; use wrapper::convert::ToLua; @@ -24,6 +25,10 @@ quick_error! { display("custom serialization error: {}", msg) description("custom serialization error") } + IntegerTooLarge(v: u64) { + display("integer {} is too large for lua", v) + description("integer is too large for lua") + } } } @@ -175,34 +180,48 @@ impl<'a> Serializer for LuaSerializer<'a> { unimplemented!(); } fn serialize_i8(self, v: i8) -> Result { - unimplemented!(); + self.0.push_integer(v as i64); + Ok(()) } fn serialize_i16(self, v: i16) -> Result { - unimplemented!(); + self.0.push_integer(v as i64); + Ok(()) } fn serialize_i32(self, v: i32) -> Result { - unimplemented!(); + self.0.push_integer(v as i64); + Ok(()) } fn serialize_i64(self, v: i64) -> Result { - unimplemented!(); + self.0.push_integer(v); + Ok(()) } fn serialize_u8(self, v: u8) -> Result { - unimplemented!(); + self.0.push_integer(v as i64); + Ok(()) } fn serialize_u16(self, v: u16) -> Result { - unimplemented!(); + self.0.push_integer(v as i64); + Ok(()) } fn serialize_u32(self, v: u32) -> Result { - unimplemented!(); + self.0.push_integer(v as i64); + Ok(()) } fn serialize_u64(self, v: u64) -> Result { - unimplemented!(); + if v <= i64::MAX as u64 { + self.0.push_integer(v as i64); + Ok(()) + } else { + Err(ErrorEnum::IntegerTooLarge(v).into()) + } } fn serialize_f32(self, v: f32) -> Result { - unimplemented!(); + self.0.push_number(v as f64); + Ok(()) } fn serialize_f64(self, v: f64) -> Result { - unimplemented!(); + self.0.push_number(v); + Ok(()) } fn serialize_char(self, v: char) -> Result { unimplemented!(); @@ -332,4 +351,31 @@ mod test { let mut state = State::new(); state.push(Serde(&"hello")); } + + #[test] + fn serialize_int() { + let mut state = State::new(); + state.push(Serde(&1i8)); + state.push(Serde(&1i16)); + state.push(Serde(&1i32)); + state.push(Serde(&1i64)); + state.push(Serde(&1u8)); + state.push(Serde(&1u16)); + state.push(Serde(&1u32)); + state.push(Serde(&1u64)); + } + + #[test] + #[should_panic(expected="IntegerTooLarge")] + fn serialize_big_int() { + let mut state = State::new(); + state.push(Serde(&10000000000000000000u64)); + } + + #[test] + fn serialize_float() { + let mut state = State::new(); + state.push(Serde(&1.0f32)); + state.push(Serde(&1.0f64)); + } } From 166d2df49d7551a23947da3b361da653cded3aca Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 16:20:22 +0300 Subject: [PATCH 03/11] serde for char --- src/wrapper/serde.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 6125699..6bba6a1 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -224,7 +224,8 @@ impl<'a> Serializer for LuaSerializer<'a> { Ok(()) } fn serialize_char(self, v: char) -> Result { - unimplemented!(); + self.0.push_string(&v.to_string()); + Ok(()) } fn serialize_str(self, v: &str) -> Result { self.0.push_string(v); @@ -378,4 +379,10 @@ mod test { state.push(Serde(&1.0f32)); state.push(Serde(&1.0f64)); } + + #[test] + fn serialize_char() { + let mut state = State::new(); + state.push(Serde(&'x')); + } } From 66c85cfc5ae3ab3326d592a0d83bc75862f0648d Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 16:22:49 +0300 Subject: [PATCH 04/11] Implements serde for option --- src/wrapper/serde.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 6bba6a1..21dcc42 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -235,7 +235,8 @@ impl<'a> Serializer for LuaSerializer<'a> { unimplemented!(); } fn serialize_none(self) -> Result { - unimplemented!(); + self.0.push_nil(); + Ok(()) } fn serialize_some( self, @@ -244,7 +245,7 @@ impl<'a> Serializer for LuaSerializer<'a> { where T: Serialize { - unimplemented!(); + value.serialize(self) } fn serialize_unit(self) -> Result { unimplemented!(); @@ -385,4 +386,11 @@ mod test { let mut state = State::new(); state.push(Serde(&'x')); } + + #[test] + fn serialize_option() { + let mut state = State::new(); + state.push(Serde(&Some("hello"))); + state.push(Serde(&None::<&str>)); + } } From 7b19eb9147dca824805620509b43a461317a908e Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 19:07:51 +0300 Subject: [PATCH 05/11] Implements serde for sequences --- src/wrapper/serde.rs | 68 +++++++++++++++++++++++++++++++++----------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 21dcc42..300953f 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::i64; +use std::{i32, i64}; use serde::{Serialize, Serializer, ser}; use wrapper::convert::ToLua; @@ -9,7 +9,11 @@ pub struct Serde<'a, S: Serialize + ?Sized + 'a>(&'a S); struct LuaSerializer<'a>(&'a mut State); -struct SerializeSeq<'a>(&'a mut State); +struct SerializeSeq<'a> { + state: &'a mut State, + table_index: i32, + current_subscript: i32 +} struct SerializeTuple<'a>(&'a mut State); struct SerializeTupleStruct<'a>(&'a mut State); struct SerializeTupleVariant<'a>(&'a mut State); @@ -29,6 +33,10 @@ quick_error! { display("integer {} is too large for lua", v) description("integer is too large for lua") } + TableSizeTooLarge(v: u64) { + display("table size {} is too large for lua", v) + description("table size is too large for lua (31 bits max)") + } } } @@ -38,21 +46,36 @@ impl ser::Error for Error { } } +impl<'a> SerializeSeq<'a> { + fn new(state: &'a mut State, prealloc: i32) -> SerializeSeq<'a> { + state.create_table(prealloc, 0); + SerializeSeq { + table_index: state.get_top(), + current_subscript: 0, + state, + } + } +} impl<'a> ser::SerializeSeq for SerializeSeq<'a> { type Ok = (); type Error = Error; - fn serialize_element( - &mut self, - value: &T - ) -> Result<(), Self::Error> - where - T: Serialize + fn serialize_element(&mut self, value: &T) + -> Result<(), Self::Error> + where T: Serialize { - unimplemented!(); + if self.current_subscript == i32::MAX { + return Err(ErrorEnum::TableSizeTooLarge( + self.current_subscript as u64).into()); + } + self.current_subscript += 1; + value.serialize(LuaSerializer(self.state)); + self.state.raw_seti(self.table_index, self.current_subscript as i64); + Ok(()) } fn end(self) -> Result { - unimplemented!(); + // table is already at the top of the stack + Ok(()) } } @@ -281,14 +304,18 @@ impl<'a> Serializer for LuaSerializer<'a> { value: &T ) -> Result where - T: Serialize { + T: Serialize + { unimplemented!(); } - fn serialize_seq( - self, - len: Option - ) -> Result { - unimplemented!(); + fn serialize_seq(self, len: Option) + -> Result + { + if len.map(|x| x <= i32::MAX as usize).unwrap_or(true) { + Ok(SerializeSeq::new(self.0, len.map(|x| x as i32).unwrap_or(0))) + } else { + Err(ErrorEnum::IntegerTooLarge(len.unwrap() as u64).into()) + } } fn serialize_tuple( self, @@ -393,4 +420,13 @@ mod test { state.push(Serde(&Some("hello"))); state.push(Serde(&None::<&str>)); } + + #[test] + fn serialize_list() { + let mut state = State::new(); + let x: &[u32] = &[1, 2, 3]; + state.push(Serde(&x)); + let x = vec![1, 2, 3]; + state.push(Serde(&x)); + } } From 1c7146ae191f4df900a51f3f18672df1dbbc4459 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 19:26:25 +0300 Subject: [PATCH 06/11] Implements serde for maps --- src/wrapper/serde.rs | 45 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 300953f..18e104c 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -17,7 +17,10 @@ struct SerializeSeq<'a> { struct SerializeTuple<'a>(&'a mut State); struct SerializeTupleStruct<'a>(&'a mut State); struct SerializeTupleVariant<'a>(&'a mut State); -struct SerializeMap<'a>(&'a mut State); +struct SerializeMap<'a> { + state: &'a mut State, + table_index: i32, +} struct SerializeStruct<'a>(&'a mut State); struct SerializeStructVariant<'a>(&'a mut State); @@ -130,6 +133,16 @@ impl<'a> ser::SerializeTupleVariant for SerializeTupleVariant<'a> { } } +impl<'a> SerializeMap<'a> { + fn new(state: &'a mut State, prealloc: i32) -> SerializeMap<'a> { + state.create_table(0, prealloc); + SerializeMap { + table_index: state.get_top(), + state, + } + } +} + impl<'a> ser::SerializeMap for SerializeMap<'a> { type Ok = (); type Error = Error; @@ -137,7 +150,7 @@ impl<'a> ser::SerializeMap for SerializeMap<'a> { where T: Serialize { - unimplemented!(); + key.serialize(LuaSerializer(self.state)) } fn serialize_value( &mut self, @@ -146,10 +159,13 @@ impl<'a> ser::SerializeMap for SerializeMap<'a> { where T: Serialize { - unimplemented!(); + value.serialize(LuaSerializer(self.state))?; + self.state.raw_set(self.table_index); + Ok(()) } fn end(self) -> Result { - unimplemented!(); + // table is already at the top of the stack + Ok(()) } } @@ -339,11 +355,10 @@ impl<'a> Serializer for LuaSerializer<'a> { ) -> Result { unimplemented!(); } - fn serialize_map( - self, - len: Option - ) -> Result { - unimplemented!(); + fn serialize_map(self, len: Option) + -> Result + { + Ok(SerializeMap::new(self.0, len.map(|x| x as i32).unwrap_or(0))) } fn serialize_struct( self, @@ -372,6 +387,8 @@ impl<'a, T: Serialize + 'a> ToLua for Serde<'a, T> { #[cfg(test)] mod test { + use std::collections::HashMap; + use {State}; use super::Serde; @@ -429,4 +446,14 @@ mod test { let x = vec![1, 2, 3]; state.push(Serde(&x)); } + + #[test] + fn serialize_map() { + let mut state = State::new(); + let x = vec![ + ("x", 1), + ("y", 2), + ].into_iter().collect::>(); + state.push(Serde(&x)); + } } From aef7863c7d5e536c213a81ba1293d98347d25bf8 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 19:37:59 +0300 Subject: [PATCH 07/11] Implements serde for structs --- src/wrapper/serde.rs | 61 ++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 16 deletions(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 18e104c..8979bf4 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -14,14 +14,21 @@ struct SerializeSeq<'a> { table_index: i32, current_subscript: i32 } + struct SerializeTuple<'a>(&'a mut State); struct SerializeTupleStruct<'a>(&'a mut State); struct SerializeTupleVariant<'a>(&'a mut State); + struct SerializeMap<'a> { state: &'a mut State, table_index: i32, } -struct SerializeStruct<'a>(&'a mut State); + +struct SerializeStruct<'a> { + state: &'a mut State, + table_index: i32, +} + struct SerializeStructVariant<'a>(&'a mut State); @@ -169,21 +176,30 @@ impl<'a> ser::SerializeMap for SerializeMap<'a> { } } +impl<'a> SerializeStruct<'a> { + fn new(state: &'a mut State, fields: i32) -> SerializeStruct<'a> { + state.create_table(0, fields); + SerializeStruct { + table_index: state.get_top(), + state, + } + } +} + impl<'a> ser::SerializeStruct for SerializeStruct<'a> { type Ok = (); type Error = Error; - fn serialize_field( - &mut self, - key: &'static str, - value: &T - ) -> Result<(), Self::Error> - where - T: Serialize + fn serialize_field(&mut self, key: &'static str, value: &T) + -> Result<(), Self::Error> + where T: Serialize { - unimplemented!(); + value.serialize(LuaSerializer(self.state))?; + self.state.set_field(self.table_index, key); + Ok(()) } fn end(self) -> Result { - unimplemented!(); + // table is already at the top of the stack + Ok(()) } } @@ -360,12 +376,14 @@ impl<'a> Serializer for LuaSerializer<'a> { { Ok(SerializeMap::new(self.0, len.map(|x| x as i32).unwrap_or(0))) } - fn serialize_struct( - self, - name: &'static str, - len: usize - ) -> Result { - unimplemented!(); + fn serialize_struct(self, name: &'static str, len: usize) + -> Result + { + if len <= i32::MAX as usize { + Ok(SerializeStruct::new(self.0, len as i32)) + } else { + return Err(ErrorEnum::TableSizeTooLarge(len as u64).into()); + } } fn serialize_struct_variant( self, @@ -388,6 +406,7 @@ impl<'a, T: Serialize + 'a> ToLua for Serde<'a, T> { #[cfg(test)] mod test { use std::collections::HashMap; + use std::time::Duration; use {State}; use super::Serde; @@ -456,4 +475,14 @@ mod test { ].into_iter().collect::>(); state.push(Serde(&x)); } + + #[test] + fn serialize_struct() { + let mut state = State::new(); + // Duration is serialized as a struct with two fields + // so we can test it without using `serde_derive` + state.push(Serde(&Duration::from_millis(12345))); + } + + } From ded3380a8beab393bc42d05f24d2a2d4e94c72d0 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 19:44:08 +0300 Subject: [PATCH 08/11] Eliminates most warnings in serde code --- src/lib.rs | 3 ++ src/wrapper/mod.rs | 2 +- src/wrapper/serde.rs | 126 +++++++++++++++++-------------------------- 3 files changed, 53 insertions(+), 78 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cca0d22..d53f230 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,6 +60,9 @@ pub use wrapper::convert::{ FromLua }; +#[cfg(feature="serialize")] +pub use wrapper::serde::Serde; + pub use ffi::lua_Number as Number; pub use ffi::lua_Integer as Integer; pub use ffi::lua_CFunction as Function; diff --git a/src/wrapper/mod.rs b/src/wrapper/mod.rs index 28e1b12..3aecf49 100644 --- a/src/wrapper/mod.rs +++ b/src/wrapper/mod.rs @@ -25,5 +25,5 @@ pub mod convert; pub mod state; #[cfg(feature="serialize")] -mod serde; +pub mod serde; diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 8979bf4..fde81a6 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -79,7 +79,7 @@ impl<'a> ser::SerializeSeq for SerializeSeq<'a> { self.current_subscript as u64).into()); } self.current_subscript += 1; - value.serialize(LuaSerializer(self.state)); + value.serialize(LuaSerializer(self.state))?; self.state.raw_seti(self.table_index, self.current_subscript as i64); Ok(()) } @@ -92,12 +92,9 @@ impl<'a> ser::SerializeSeq for SerializeSeq<'a> { impl<'a> ser::SerializeTuple for SerializeTuple<'a> { type Ok = (); type Error = Error; - fn serialize_element( - &mut self, - value: &T - ) -> Result<(), Self::Error> - where - T: Serialize + fn serialize_element(&mut self, _value: &T) + -> Result<(), Self::Error> + where T: Serialize { unimplemented!(); } @@ -109,12 +106,9 @@ impl<'a> ser::SerializeTuple for SerializeTuple<'a> { impl<'a> ser::SerializeTupleStruct for SerializeTupleStruct<'a> { type Ok = (); type Error = Error; - fn serialize_field( - &mut self, - value: &T - ) -> Result<(), Self::Error> - where - T: Serialize + fn serialize_field(&mut self, _value: &T) + -> Result<(), Self::Error> + where T: Serialize { unimplemented!(); } @@ -126,12 +120,9 @@ impl<'a> ser::SerializeTupleStruct for SerializeTupleStruct<'a> { impl<'a> ser::SerializeTupleVariant for SerializeTupleVariant<'a> { type Ok = (); type Error = Error; - fn serialize_field( - &mut self, - value: &T - ) -> Result<(), Self::Error> - where - T: Serialize + fn serialize_field(&mut self, _value: &T) + -> Result<(), Self::Error> + where T: Serialize { unimplemented!(); } @@ -206,13 +197,9 @@ impl<'a> ser::SerializeStruct for SerializeStruct<'a> { impl<'a> ser::SerializeStructVariant for SerializeStructVariant<'a> { type Ok = (); type Error = Error; - fn serialize_field( - &mut self, - key: &'static str, - value: &T - ) -> Result<(), Self::Error> - where - T: Serialize + fn serialize_field(&mut self, _key: &'static str, _value: &T) + -> Result<(), Self::Error> + where T: Serialize { unimplemented!(); } @@ -286,7 +273,7 @@ impl<'a> Serializer for LuaSerializer<'a> { self.0.push_string(v); Ok(()) } - fn serialize_bytes(self, v: &[u8]) -> Result { + fn serialize_bytes(self, _v: &[u8]) -> Result { unimplemented!(); } fn serialize_none(self) -> Result { @@ -305,38 +292,29 @@ impl<'a> Serializer for LuaSerializer<'a> { fn serialize_unit(self) -> Result { unimplemented!(); } - fn serialize_unit_struct( - self, - name: &'static str - ) -> Result { + fn serialize_unit_struct(self, _name: &'static str) + -> Result + { unimplemented!(); } - fn serialize_unit_variant( - self, - name: &'static str, - variant_index: u32, - variant: &'static str - ) -> Result { + fn serialize_unit_variant(self, + _name: &'static str, _variant_index: u32, _variant: &'static str) + -> Result + { unimplemented!(); } - fn serialize_newtype_struct( - self, - name: &'static str, - value: &T - ) -> Result - where - T: Serialize { + fn serialize_newtype_struct(self, + _name: &'static str, _value: &T) + -> Result + where T: Serialize + { unimplemented!(); } - fn serialize_newtype_variant( - self, - name: &'static str, - variant_index: u32, - variant: &'static str, - value: &T - ) -> Result - where - T: Serialize + fn serialize_newtype_variant(self, + _name: &'static str, _variant_index: u32, _variant: &'static str, + _value: &T + ) -> Result + where T: Serialize { unimplemented!(); } @@ -349,26 +327,22 @@ impl<'a> Serializer for LuaSerializer<'a> { Err(ErrorEnum::IntegerTooLarge(len.unwrap() as u64).into()) } } - fn serialize_tuple( - self, - len: usize - ) -> Result { + fn serialize_tuple(self, _len: usize) + -> Result + { unimplemented!(); } - fn serialize_tuple_struct( - self, - name: &'static str, - len: usize - ) -> Result { + fn serialize_tuple_struct(self, + _name: &'static str, _len: usize) + -> Result + { unimplemented!(); } - fn serialize_tuple_variant( - self, - name: &'static str, - variant_index: u32, - variant: &'static str, - len: usize - ) -> Result { + fn serialize_tuple_variant(self, + _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize) + -> Result + { unimplemented!(); } fn serialize_map(self, len: Option) @@ -376,7 +350,7 @@ impl<'a> Serializer for LuaSerializer<'a> { { Ok(SerializeMap::new(self.0, len.map(|x| x as i32).unwrap_or(0))) } - fn serialize_struct(self, name: &'static str, len: usize) + fn serialize_struct(self, _name: &'static str, len: usize) -> Result { if len <= i32::MAX as usize { @@ -385,13 +359,11 @@ impl<'a> Serializer for LuaSerializer<'a> { return Err(ErrorEnum::TableSizeTooLarge(len as u64).into()); } } - fn serialize_struct_variant( - self, - name: &'static str, - variant_index: u32, - variant: &'static str, - len: usize - ) -> Result { + fn serialize_struct_variant(self, + _name: &'static str, _variant_index: u32, _variant: &'static str, + _len: usize) + -> Result + { unimplemented!(); } } From 8aac2a9acdba8b89b37a06be2df4928afd5d4ee8 Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 19:45:53 +0300 Subject: [PATCH 09/11] Implements serde for boolean --- src/wrapper/serde.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index fde81a6..6cd663a 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -219,7 +219,8 @@ impl<'a> Serializer for LuaSerializer<'a> { type SerializeStruct = SerializeStruct<'a>; type SerializeStructVariant = SerializeStructVariant<'a>; fn serialize_bool(self, v: bool) -> Result { - unimplemented!(); + self.0.push_bool(v); + Ok(()) } fn serialize_i8(self, v: i8) -> Result { self.0.push_integer(v as i64); @@ -383,6 +384,13 @@ mod test { use {State}; use super::Serde; + #[test] + fn serialize_bool() { + let mut state = State::new(); + state.push(Serde(&true)); + state.push(Serde(&false)); + } + #[test] fn serialize_str() { let mut state = State::new(); From 4762ab6a1bd6c2ad13a0f7ce254a8c8ac85e0e3b Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Tue, 6 Jun 2017 19:48:01 +0300 Subject: [PATCH 10/11] Implements serde for `()` --- src/wrapper/serde.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index 6cd663a..e49a051 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -291,7 +291,8 @@ impl<'a> Serializer for LuaSerializer<'a> { value.serialize(self) } fn serialize_unit(self) -> Result { - unimplemented!(); + self.0.push_nil(); + Ok(()) } fn serialize_unit_struct(self, _name: &'static str) -> Result @@ -391,6 +392,12 @@ mod test { state.push(Serde(&false)); } + #[test] + fn serialize_unit() { + let mut state = State::new(); + state.push(Serde(&())); + } + #[test] fn serialize_str() { let mut state = State::new(); From 90d38a8919389c1daf2fb2329ac183b4d59f48bf Mon Sep 17 00:00:00 2001 From: Paul Colomiets Date: Wed, 7 Jun 2017 16:18:07 +0300 Subject: [PATCH 11/11] Exposes serde wrapper as having a public field --- src/wrapper/serde.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wrapper/serde.rs b/src/wrapper/serde.rs index e49a051..ecf544d 100644 --- a/src/wrapper/serde.rs +++ b/src/wrapper/serde.rs @@ -5,7 +5,7 @@ use serde::{Serialize, Serializer, ser}; use wrapper::convert::ToLua; use wrapper::state::State; -pub struct Serde<'a, S: Serialize + ?Sized + 'a>(&'a S); +pub struct Serde<'a, S: Serialize + ?Sized + 'a>(pub &'a S); struct LuaSerializer<'a>(&'a mut State);