Crate kevy_lua_host
Expand description
Scoped-borrow bridge between kevy_lua::Bridge and a host-owned
mutable shard state (Store, KeyspaceStore, anything 'static).
kevy-lua’s dispatch closure type is
Fn(&[&[u8]], bool) -> Vec<u8> + 'static. The 'static bound is
mandatory — luna stores the closure as Vm userdata (Any + 'static).
That makes it impossible to capture &mut T directly. This crate
offers a tiny LuaHost<T> wrapper that re-introduces the borrow via
a scoped thread-local pointer set inside LuaHost::eval and cleared
right after. The dispatch closure consults the pointer.
§Safety contract (read this if you touch the unsafe)
LuaHost<T>parameterises over the host context typeT(kevy’sStorefor the production wiring; an arbitrary type in tests).LuaHost::new(dispatch_fn)builds a kevy-luaBridgewhose dispatch closure doeswith_current::<T>(|t| dispatch_fn(t, argv, ro)). The closure carries NO captured state of its own — it just reads the scoped pointer.LuaHost::eval(&mut self, &mut T, …)(and friends) setCURRENT_T = ctx as *mut TBEFORE delegating toBridge::eval, and CLEARCURRENT_T = nullafter. ADropguard ensures the clear even on panic.- Inside the dispatch closure,
with_currentdereferencesCURRENT_Texactly once per call. The pointer is only ever non-null while the outer&mut Tis borrowed mutably byLuaHost::eval, so no aliasing exists. - kevy is single-threaded per-shard — every shard owns its own
LuaHost<T>and runs on a dedicated thread. The thread-local gives correct isolation without any synchronisation overhead.
The unsafe footprint is one unsafe { &mut *p } inside
with_current plus the Cell::set(ptr) ergonomics. Audited per
every kevy v1.27+ commit touching this file.
Structs§
- LuaHost
- kevy-side per-shard Lua host. Wraps a
kevy_lua::Bridgeplus the scoped-pointer plumbing.
Functions§
- with_
current - Run
fwith a mutable borrow of the currently-set host context. ReturnsNoneifLuaHost::evalisn’t on the stack.