From b879abc418475a375065c2156ef46c9c1aeaff80 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Tue, 24 Oct 2023 23:57:11 +0100 Subject: Add lua_newuserdata_t helper to mlua-sys/luau --- mlua-sys/src/luau/lua.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'mlua-sys') diff --git a/mlua-sys/src/luau/lua.rs b/mlua-sys/src/luau/lua.rs index 596db0c..996f62e 100644 --- a/mlua-sys/src/luau/lua.rs +++ b/mlua-sys/src/luau/lua.rs @@ -2,7 +2,7 @@ use std::marker::{PhantomData, PhantomPinned}; use std::os::raw::{c_char, c_double, c_float, c_int, c_uint, c_void}; -use std::ptr; +use std::{mem, ptr}; // Option for multiple returns in 'lua_pcall' and 'lua_call' pub const LUA_MULTRET: c_int = -1; @@ -326,6 +326,15 @@ pub unsafe fn lua_newuserdata(L: *mut lua_State, sz: usize) -> *mut c_void { lua_newuserdatatagged(L, sz, 0) } +#[inline(always)] +pub unsafe fn lua_newuserdata_t(L: *mut lua_State) -> *mut T { + unsafe extern "C-unwind" fn destructor(ud: *mut c_void) { + ptr::drop_in_place(ud as *mut T); + } + + lua_newuserdatadtor(L, mem::size_of::(), destructor::) as *mut T +} + // TODO: lua_strlen #[inline(always)] -- cgit v1.2.3