mlua-rs / mlua

High level Lua 5.4/5.3/5.2/5.1 (including LuaJIT) and Roblox Luau bindings to Rust with async/await support
Other
1.75k stars 139 forks source link

Get `registry_id` from `RegistryKey` #422

Closed nferhat closed 5 months ago

nferhat commented 5 months ago

Hi! Currently using this library to integrate configuration and scripting into my program.

Some of my usage has signals (basically callbacks run on specific events), storing them in: Vec<RegistryKey> (where the registry key points to a function in the lua state)

If I want to remove a key, I don't want to rely on vector indexes in order to do so. A possible solution is to use registry_id to remove using Vec::retain

This property is private, one (hacky and bad) way to get it out is using:

fn get_registry_id(key: &mlua::RegistryKey) -> u64 {
    let mut hasher = std::hash::DefaultHasher::new();
    key.hash(&mut hasher);
    hasher.finish()
}

A getter would be much appreciated.

nferhat commented 5 months ago

UPDATE: That function get_registry_id does not get the registry id, just a hashed form

khvzak commented 5 months ago

I added RegistryKey::id() method in this commit but note if you use Lua::replace_registry_value the underlying key can be changed.

In next major release I'm planning to take &mut instead when replacing values to better handle this.

nferhat commented 5 months ago

Thank you very much, exactly what I needed!

I don't use replace_registry_value, so changing registry ids should not be a concern (hopefully).

:+1: