Cannot return HashMap value. #210
Answered
by
khvzak
irishgreencitrus
asked this question in
Q&A
-
I have two structs. One holds a HashMap of a String to the other. When I try to implement a method getting the value of the hashmap from a string, it only returns a borrow, and I cannot implement the struct PackageManager {
install_callback: RegistryKey,
packages: Vec<String>,
}
struct PackageScope {
builtins: HashMap<String, PackageManager>,
user: String,
}
impl UserData for PackageManager { ... }
impl UserData for PackageScope {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(LuaMetaMethod::Index, |lua, this: &Self, key: String| {
let ret = this.builtins.get(&key);
return match ret {
Some(val) => Ok(val),
None => Err(LuaError::RuntimeError("Invalid key".to_string()))
};
});
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
khvzak
Oct 4, 2022
Replies: 1 comment 1 reply
-
There are few options:
struct PackageScope {
builtins: HashMap<String, Rc<RefCell<PackageManager>>>,
user: String,
} Then you can send it to Lua (cloned by reference).
struct PackageScope {
builtins: HashMap<String, RegistryKey>,
user: String,
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
irishgreencitrus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There are few options:
Rc<RefCell>
:Then you can send it to Lua (cloned by reference).
Store
RegistryKey
asRc<RegistryKey>
, this would allow to implementClone
forPackageManager
Store
PackageManager
directly inside Lua and refer to each instance viaRegistryKey
, eg: