Open
Description
I want to implement copy-on-write for a add_field_function_get()
: I have a File
that remains immutable throughout Lua::scope()
, and thus its url
property is also immutable, and since that url
property is frequently used, I cache it as follows:
fields.add_field_function_get("url", |lua, ud| {
ud.borrow_mut_scoped(|me: &mut Self| {
Ok(match &me.cache {
Some(v) => v.clone(), // Reuse the cached `AnyUserData`
None => { // Create a new `AnyUserData` and cache it
me.cache = Some(lua.create_any_userdata(me.url.clone())?);
me.cache.clone().unwrap()
}
})
})?
});
For some Lua APIs that merely accept a borrowed UserData value (UserDataRef<Url>
), this works well:
fs.metadata(file.url)
However, there are some APIs that accept an owned UserData, so they take ownership of the Url
:
fs.File { url = file.url, ... }
fs.File
will call AnyUserData::take()
to take ownership of the Url
, which leads to me.cache
being destructed. So, I would like a way to check whether the UserData
is still available:
- Ok(match &me.cache {
+ Ok(match me.cache.as_ref().filter(|ud| !ud.is_destructed()) {
Perhaps is_destroyed()
is a better name than is_destructed()
in order to be consistent with the existing UserData::destroy()
.
Metadata
Metadata
Assignees
Labels
No labels