diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2022-02-14 20:51:24 +0000 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2022-02-14 20:51:24 +0000 |
commit | 6190427f373b24a12ab527704fab56561da11316 (patch) | |
tree | a7ec0653c8e7c92ee7c6f367ee3e1a9cf37952a9 /src/lua.rs | |
parent | 9a5a341e44eb28f6f78d013b1ff9162f79eff163 (diff) | |
download | mlua-6190427f373b24a12ab527704fab56561da11316.zip |
Add Lua::replace_registry_value
Diffstat (limited to 'src/lua.rs')
-rw-r--r-- | src/lua.rs | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -1777,6 +1777,33 @@ impl Lua { Ok(()) } + /// Replaces a value in the Lua registry by its `RegistryKey`. + /// + /// See [`create_registry_value`] for more details. + /// + /// [`create_registry_value`]: #method.create_registry_value + pub fn replace_registry_value<'lua, T: ToLua<'lua>>( + &'lua self, + key: &RegistryKey, + t: T, + ) -> Result<()> { + let t = t.to_lua(self)?; + unsafe { + let _sg = StackGuard::new(self.state); + check_stack(self.state, 4)?; + + self.push_value(t)?; + // It must be safe to replace the value without triggering memory error + ffi::lua_rawseti( + self.state, + ffi::LUA_REGISTRYINDEX, + key.registry_id as Integer, + ); + + Ok(()) + } + } + /// Returns true if the given `RegistryKey` was created by a `Lua` which shares the underlying /// main state with this `Lua` instance. /// |