Remove Result from lua.null() and lua.array_metatable(). They never fail.

This commit is contained in:
Alex Orlenko 2021-04-27 00:29:24 +01:00
parent bc81d1016f
commit 2fae94586d
5 changed files with 21 additions and 22 deletions

View File

@ -4,7 +4,7 @@ use mlua::{Error, Lua, LuaSerdeExt, Result};
async fn main() -> Result<()> { async fn main() -> Result<()> {
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
globals.set("null", lua.null()?)?; globals.set("null", lua.null())?;
let fetch_json = lua.create_async_function(|lua, uri: String| async move { let fetch_json = lua.create_async_function(|lua, uri: String| async move {
let resp = reqwest::get(&uri) let resp = reqwest::get(&uri)

View File

@ -33,8 +33,8 @@ fn main() -> Result<()> {
"#).eval()?)?; "#).eval()?)?;
// Set it as (serializable) userdata // Set it as (serializable) userdata
globals.set("null", lua.null()?)?; globals.set("null", lua.null())?;
globals.set("array_mt", lua.array_metatable()?)?; globals.set("array_mt", lua.array_metatable())?;
globals.set("car", lua.create_ser_userdata(car)?)?; globals.set("car", lua.create_ser_userdata(car)?)?;
// Create a Lua table with multiple data types // Create a Lua table with multiple data types

View File

@ -26,7 +26,7 @@ pub trait LuaSerdeExt<'lua> {
/// ///
/// fn main() -> Result<()> { /// fn main() -> Result<()> {
/// let lua = Lua::new(); /// let lua = Lua::new();
/// lua.globals().set("null", lua.null()?)?; /// lua.globals().set("null", lua.null())?;
/// ///
/// let val = lua.load(r#"{a = null}"#).eval()?; /// let val = lua.load(r#"{a = null}"#).eval()?;
/// let map: HashMap<String, Option<String>> = lua.from_value(val)?; /// let map: HashMap<String, Option<String>> = lua.from_value(val)?;
@ -35,7 +35,7 @@ pub trait LuaSerdeExt<'lua> {
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
fn null(&'lua self) -> Result<Value<'lua>>; fn null(&'lua self) -> Value<'lua>;
/// A metatable attachable to a Lua table to systematically encode it as Array (instead of Map). /// A metatable attachable to a Lua table to systematically encode it as Array (instead of Map).
/// As result, encoded Array will contain only sequence part of the table, with the same length /// As result, encoded Array will contain only sequence part of the table, with the same length
@ -51,7 +51,7 @@ pub trait LuaSerdeExt<'lua> {
/// ///
/// fn main() -> Result<()> { /// fn main() -> Result<()> {
/// let lua = Lua::new(); /// let lua = Lua::new();
/// lua.globals().set("array_mt", lua.array_metatable()?)?; /// lua.globals().set("array_mt", lua.array_metatable())?;
/// ///
/// // Encode as an empty array (no sequence part in the lua table) /// // Encode as an empty array (no sequence part in the lua table)
/// let val = lua.load("setmetatable({a = 5}, array_mt)").eval()?; /// let val = lua.load("setmetatable({a = 5}, array_mt)").eval()?;
@ -66,7 +66,7 @@ pub trait LuaSerdeExt<'lua> {
/// Ok(()) /// Ok(())
/// } /// }
/// ``` /// ```
fn array_metatable(&'lua self) -> Result<Table<'lua>>; fn array_metatable(&'lua self) -> Table<'lua>;
/// Converts `T` into a `Value` instance. /// Converts `T` into a `Value` instance.
/// ///
@ -162,19 +162,18 @@ pub trait LuaSerdeExt<'lua> {
} }
impl<'lua> LuaSerdeExt<'lua> for Lua { impl<'lua> LuaSerdeExt<'lua> for Lua {
fn null(&'lua self) -> Result<Value<'lua>> { fn null(&'lua self) -> Value<'lua> {
// TODO: Remove Result? Value::LightUserData(LightUserData(ptr::null_mut()))
Ok(Value::LightUserData(LightUserData(ptr::null_mut())))
} }
fn array_metatable(&'lua self) -> Result<Table<'lua>> { fn array_metatable(&'lua self) -> Table<'lua> {
unsafe { unsafe {
let _sg = StackGuard::new(self.state); let _sg = StackGuard::new(self.state);
assert_stack(self.state, 1); assert_stack(self.state, 1);
push_array_metatable(self.state); push_array_metatable(self.state);
Ok(Table(self.pop_ref())) Table(self.pop_ref())
} }
} }

View File

@ -128,7 +128,7 @@ impl<'lua> ser::Serializer for Serializer<'lua> {
#[inline] #[inline]
fn serialize_none(self) -> Result<Value<'lua>> { fn serialize_none(self) -> Result<Value<'lua>> {
if self.options.serialize_none_to_null { if self.options.serialize_none_to_null {
self.lua.null() Ok(self.lua.null())
} else { } else {
Ok(Value::Nil) Ok(Value::Nil)
} }
@ -145,7 +145,7 @@ impl<'lua> ser::Serializer for Serializer<'lua> {
#[inline] #[inline]
fn serialize_unit(self) -> Result<Value<'lua>> { fn serialize_unit(self) -> Result<Value<'lua>> {
if self.options.serialize_unit_to_null { if self.options.serialize_unit_to_null {
self.lua.null() Ok(self.lua.null())
} else { } else {
Ok(Value::Nil) Ok(Value::Nil)
} }
@ -154,7 +154,7 @@ impl<'lua> ser::Serializer for Serializer<'lua> {
#[inline] #[inline]
fn serialize_unit_struct(self, _name: &'static str) -> Result<Value<'lua>> { fn serialize_unit_struct(self, _name: &'static str) -> Result<Value<'lua>> {
if self.options.serialize_unit_to_null { if self.options.serialize_unit_to_null {
self.lua.null() Ok(self.lua.null())
} else { } else {
Ok(Value::Nil) Ok(Value::Nil)
} }
@ -201,7 +201,7 @@ impl<'lua> ser::Serializer for Serializer<'lua> {
let len = len.unwrap_or(0) as c_int; let len = len.unwrap_or(0) as c_int;
let table = self.lua.create_table_with_capacity(len, 0)?; let table = self.lua.create_table_with_capacity(len, 0)?;
if self.options.set_array_metatable { if self.options.set_array_metatable {
table.set_metatable(Some(self.lua.array_metatable()?)); table.set_metatable(Some(self.lua.array_metatable()));
} }
let options = self.options; let options = self.options;
Ok(SerializeVec { table, options }) Ok(SerializeVec { table, options })

View File

@ -17,10 +17,10 @@ fn test_serialize() -> Result<(), Box<dyn std::error::Error>> {
let ud = lua.create_ser_userdata(MyUserData(123, "test userdata".into()))?; let ud = lua.create_ser_userdata(MyUserData(123, "test userdata".into()))?;
globals.set("ud", ud)?; globals.set("ud", ud)?;
globals.set("null", lua.null()?)?; globals.set("null", lua.null())?;
let empty_array = lua.create_table()?; let empty_array = lua.create_table()?;
empty_array.set_metatable(Some(lua.array_metatable()?)); empty_array.set_metatable(Some(lua.array_metatable()));
globals.set("empty_array", empty_array)?; globals.set("empty_array", empty_array)?;
let val = lua let val = lua
@ -145,7 +145,7 @@ fn test_serialize_failure() -> Result<(), Box<dyn std::error::Error>> {
fn test_to_value_struct() -> LuaResult<()> { fn test_to_value_struct() -> LuaResult<()> {
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
globals.set("null", lua.null()?)?; globals.set("null", lua.null())?;
#[derive(Serialize)] #[derive(Serialize)]
struct Test { struct Test {
@ -175,7 +175,7 @@ fn test_to_value_struct() -> LuaResult<()> {
fn test_to_value_enum() -> LuaResult<()> { fn test_to_value_enum() -> LuaResult<()> {
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
globals.set("null", lua.null()?)?; globals.set("null", lua.null())?;
#[derive(Serialize)] #[derive(Serialize)]
struct Test { struct Test {
@ -236,7 +236,7 @@ fn test_to_value_enum() -> LuaResult<()> {
fn test_to_value_with_options() -> Result<(), Box<dyn std::error::Error>> { fn test_to_value_with_options() -> Result<(), Box<dyn std::error::Error>> {
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals(); let globals = lua.globals();
globals.set("null", lua.null()?)?; globals.set("null", lua.null())?;
// set_array_metatable // set_array_metatable
let data = lua.to_value_with( let data = lua.to_value_with(
@ -386,7 +386,7 @@ fn test_from_value_enum() -> Result<(), Box<dyn std::error::Error>> {
#[test] #[test]
fn test_from_value_enum_untagged() -> Result<(), Box<dyn std::error::Error>> { fn test_from_value_enum_untagged() -> Result<(), Box<dyn std::error::Error>> {
let lua = Lua::new(); let lua = Lua::new();
lua.globals().set("null", lua.null()?)?; lua.globals().set("null", lua.null())?;
#[derive(Deserialize, PartialEq, Debug)] #[derive(Deserialize, PartialEq, Debug)]
#[serde(untagged)] #[serde(untagged)]