From 6d17a383df28becd07b28403b7762c4a37e7da1b Mon Sep 17 00:00:00 2001 From: kyren Date: Wed, 26 Sep 2018 21:07:11 -0400 Subject: [PATCH] Avoid mem::uninitialized in generic context as it is unsound with e.g. bool --- src/util.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/util.rs b/src/util.rs index 596e45a..5723f33 100644 --- a/src/util.rs +++ b/src/util.rs @@ -104,20 +104,26 @@ where F: Fn(*mut ffi::lua_State) -> R, R: Copy, { - struct Params { + union URes { + uninit: (), + init: R, + } + + struct Params { function: F, - result: R, + result: URes, nresults: c_int, } unsafe extern "C" fn do_call(state: *mut ffi::lua_State) -> c_int where + R: Copy, F: Fn(*mut ffi::lua_State) -> R, { let params = ffi::lua_touserdata(state, -1) as *mut Params; ffi::lua_pop(state, 1); - (*params).result = ((*params).function)(state); + (*params).result.init = ((*params).function)(state); if (*params).nresults == ffi::LUA_MULTRET { ffi::lua_gettop(state) @@ -136,7 +142,7 @@ where let mut params = Params { function: f, - result: mem::uninitialized(), + result: URes { uninit: () }, nresults, }; @@ -147,7 +153,7 @@ where if ret == ffi::LUA_OK { // LUA_OK is only returned when the do_call function has completed successfully, so // params.result is definitely initialized. - Ok(params.result) + Ok(params.result.init) } else { Err(pop_error(state, ret)) }