diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2023-07-09 14:58:37 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2023-07-09 22:11:26 +0100 |
commit | 1c20494158e7ac10e8edda2c510b2168ff5de046 (patch) | |
tree | 836a91d5100fc3101ddc4e19cbd438c5103fb296 | |
parent | c9294ad642c7dec103cc82d2f37f5985a25b98ab (diff) | |
download | mlua-1c20494158e7ac10e8edda2c510b2168ff5de046.zip |
Use usize instead of c_int for table capacity (Lua::create_table_with_capacity)
-rw-r--r-- | src/lua.rs | 16 | ||||
-rw-r--r-- | src/scope.rs | 13 | ||||
-rw-r--r-- | src/serde/ser.rs | 10 | ||||
-rw-r--r-- | src/util/mod.rs | 6 |
4 files changed, 21 insertions, 24 deletions
@@ -1409,7 +1409,7 @@ impl Lua { /// `narr` is a hint for how many elements the table will have as a sequence; /// `nrec` is a hint for how many other elements the table will have. /// Lua may use these hints to preallocate memory for the new table. - pub fn create_table_with_capacity(&self, narr: c_int, nrec: c_int) -> Result<Table> { + pub fn create_table_with_capacity(&self, narr: usize, nrec: usize) -> Result<Table> { let state = self.state(); unsafe { if self.unlikely_memory_error() { @@ -1439,7 +1439,7 @@ impl Lua { let iter = iter.into_iter(); let lower_bound = iter.size_hint().0; let protect = !self.unlikely_memory_error(); - push_table(state, 0, lower_bound as c_int, protect)?; + push_table(state, 0, lower_bound, protect)?; for (k, v) in iter { self.push_value(k.into_lua(self)?)?; self.push_value(v.into_lua(self)?)?; @@ -1468,7 +1468,7 @@ impl Lua { let iter = iter.into_iter(); let lower_bound = iter.size_hint().0; let protect = !self.unlikely_memory_error(); - push_table(state, lower_bound as c_int, 0, protect)?; + push_table(state, lower_bound, 0, protect)?; for (i, v) in iter.enumerate() { self.push_value(v.into_lua(self)?)?; if protect { @@ -2523,7 +2523,7 @@ impl Lua { let metatable_nrec = registry.meta_methods.len() + registry.meta_fields.len(); #[cfg(feature = "async")] let metatable_nrec = metatable_nrec + registry.async_meta_methods.len(); - push_table(state, 0, metatable_nrec as c_int, true)?; + push_table(state, 0, metatable_nrec, true)?; for (k, m) in registry.meta_methods { self.push_value(Value::Function(self.create_callback(m)?))?; rawset_field(state, -2, MetaMethod::validate(&k)?)?; @@ -2558,7 +2558,7 @@ impl Lua { if index_type == ffi::LUA_TNIL { // Create a new table ffi::lua_pop(state, 1); - push_table(state, 0, fields_nrec as c_int, true)?; + push_table(state, 0, fields_nrec, true)?; } for (k, f) in registry.fields { self.push_value(f(self, MultiValue::new())?.pop_front().unwrap())?; @@ -2578,7 +2578,7 @@ impl Lua { let mut field_getters_index = None; let field_getters_nrec = registry.field_getters.len(); if field_getters_nrec > 0 { - push_table(state, 0, field_getters_nrec as c_int, true)?; + push_table(state, 0, field_getters_nrec, true)?; for (k, m) in registry.field_getters { self.push_value(Value::Function(self.create_callback(m)?))?; rawset_field(state, -2, &k)?; @@ -2590,7 +2590,7 @@ impl Lua { let mut field_setters_index = None; let field_setters_nrec = registry.field_setters.len(); if field_setters_nrec > 0 { - push_table(state, 0, field_setters_nrec as c_int, true)?; + push_table(state, 0, field_setters_nrec, true)?; for (k, m) in registry.field_setters { self.push_value(Value::Function(self.create_callback(m)?))?; rawset_field(state, -2, &k)?; @@ -2611,7 +2611,7 @@ impl Lua { _ => { // Create a new table ffi::lua_pop(state, 1); - push_table(state, 0, methods_nrec as c_int, true)?; + push_table(state, 0, methods_nrec, true)?; } } for (k, m) in registry.methods { diff --git a/src/scope.rs b/src/scope.rs index 02fd5b6..7a190a9 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -2,7 +2,6 @@ use std::any::Any; use std::cell::{Cell, RefCell}; use std::marker::PhantomData; use std::mem; -use std::os::raw::c_int; #[cfg(feature = "serialize")] use serde::Serialize; @@ -240,7 +239,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { #[cfg(feature = "lua54")] for i in 1..=USER_VALUE_MAXSLOT { ffi::lua_pushnil(state); - ffi::lua_setiuservalue(state, -2, i as c_int); + ffi::lua_setiuservalue(state, -2, i as _); } #[cfg(any(feature = "lua53", feature = "lua52", feature = "luau"))] { @@ -390,7 +389,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { // Prepare metatable, add meta methods first and then meta fields let meta_methods_nrec = ud_methods.meta_methods.len() + ud_fields.meta_fields.len() + 1; - push_table(state, 0, meta_methods_nrec as c_int, true)?; + push_table(state, 0, meta_methods_nrec, true)?; for (k, m) in ud_methods.meta_methods { lua.push_value(Value::Function(wrap_method(self, ud_ptr, m)?))?; @@ -405,7 +404,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { let mut field_getters_index = None; let field_getters_nrec = ud_fields.field_getters.len(); if field_getters_nrec > 0 { - push_table(state, 0, field_getters_nrec as c_int, true)?; + push_table(state, 0, field_getters_nrec, true)?; for (k, m) in ud_fields.field_getters { lua.push_value(Value::Function(wrap_method(self, ud_ptr, m)?))?; rawset_field(state, -2, &k)?; @@ -416,7 +415,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { let mut field_setters_index = None; let field_setters_nrec = ud_fields.field_setters.len(); if field_setters_nrec > 0 { - push_table(state, 0, field_setters_nrec as c_int, true)?; + push_table(state, 0, field_setters_nrec, true)?; for (k, m) in ud_fields.field_setters { lua.push_value(Value::Function(wrap_method(self, ud_ptr, m)?))?; rawset_field(state, -2, &k)?; @@ -428,7 +427,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { let methods_nrec = ud_methods.methods.len(); if methods_nrec > 0 { // Create table used for methods lookup - push_table(state, 0, methods_nrec as c_int, true)?; + push_table(state, 0, methods_nrec, true)?; for (k, m) in ud_methods.methods { lua.push_value(Value::Function(wrap_method(self, ud_ptr, m)?))?; rawset_field(state, -2, &k)?; @@ -488,7 +487,7 @@ impl<'lua, 'scope> Scope<'lua, 'scope> { #[cfg(feature = "lua54")] for i in 1..=USER_VALUE_MAXSLOT { ffi::lua_pushnil(state); - ffi::lua_setiuservalue(state, -2, i as c_int); + ffi::lua_setiuservalue(state, -2, i as _); } #[cfg(any(feature = "lua53", feature = "lua52", feature = "luau"))] { diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 9b4a3aa..6115cb2 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -1,5 +1,3 @@ -use std::os::raw::c_int; - use serde::{ser, Serialize}; use super::LuaSerdeExt; @@ -233,8 +231,7 @@ impl<'lua> ser::Serializer for Serializer<'lua> { #[inline] fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> { - 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.unwrap_or(0), 0)?; if self.options.set_array_metatable { table.set_metatable(Some(self.lua.array_metatable())); } @@ -277,10 +274,9 @@ impl<'lua> ser::Serializer for Serializer<'lua> { #[inline] fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> { - let len = len.unwrap_or(0) as c_int; Ok(SerializeMap { key: None, - table: self.lua.create_table_with_capacity(0, len)?, + table: self.lua.create_table_with_capacity(0, len.unwrap_or(0))?, options: self.options, }) } @@ -300,7 +296,7 @@ impl<'lua> ser::Serializer for Serializer<'lua> { ) -> Result<Self::SerializeStructVariant> { Ok(SerializeStructVariant { name: self.lua.create_string(variant)?, - table: self.lua.create_table_with_capacity(0, len as c_int)?, + table: self.lua.create_table_with_capacity(0, len)?, options: self.options, }) } diff --git a/src/util/mod.rs b/src/util/mod.rs index 570c332..67195d5 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -252,10 +252,12 @@ pub unsafe fn push_string(state: *mut ffi::lua_State, s: &[u8], protect: bool) - #[inline] pub unsafe fn push_table( state: *mut ffi::lua_State, - narr: c_int, - nrec: c_int, + narr: usize, + nrec: usize, protect: bool, ) -> Result<()> { + let narr: c_int = narr.try_into().unwrap_or(c_int::MAX); + let nrec: c_int = nrec.try_into().unwrap_or(c_int::MAX); if protect { protect_lua!(state, 0, 1, |state| ffi::lua_createtable(state, narr, nrec)) } else { |