mlua-rs/mlua

Get `registry_id` from `RegistryKey`

nferhat opened this issue · 3 comments

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.

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

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.

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).

👍