Skip to content

Commit eda13ab

Browse files
committed
Some small documentation improvements
Make it a bit clearer that the registry is the solution for most lifetime issues when using rlua.
1 parent ca5f77d commit eda13ab

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@ impl<'lua> Context<'lua> {
429429
///
430430
/// This value will be available to rust from all `Lua` instances which share the same main
431431
/// state.
432+
///
433+
/// The returned [`RegistryKey`] is of `'static` lifetime and is *the* main way in `rlua` of
434+
/// maintaining ownership of a Lua value outside of a [`Lua::context`] call.
435+
///
436+
/// Be warned, garbage collection of values held inside the registry is not automatic, see
437+
/// [`RegistryKey`] for more details.
438+
///
439+
/// [`RegistryKey`]: struct.RegistryKey.html
440+
/// [`Lua::context`]: struct.Lua.html#method.context
432441
pub fn create_registry_value<T: ToLua<'lua>>(self, t: T) -> Result<RegistryKey> {
433442
let t = t.to_lua(self)?;
434443
unsafe {

src/types.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ pub(crate) type Callback<'lua, 'a> =
3030
/// [`Context::remove_registry_value`], and instances not manually removed can be garbage collected
3131
/// with [`Context::expire_registry_values`].
3232
///
33-
/// Be warned, If you place this into Lua via a `UserData` type or a rust callback, it is *very
34-
/// easy* to accidentally cause reference cycles that the Lua garbage collector cannot resolve.
35-
/// Instead of placing a `RegistryKey` into a `UserData` type, prefer instead to use
36-
/// [`UserData::set_user_value`] / [`UserData::get_user_value`], and instead of moving a RegistryKey
37-
/// into a callback, prefer [`Context::scope`].
33+
/// Be warned, If you place this into Lua via a `UserData` type or a rust callback and rely on
34+
/// [`Context::expire_registry_values`], it is *very easy* to accidentally cause reference cycles
35+
/// that cannot be automatically collected. The Lua garbage collector is not aware of the registry
36+
/// handle pattern, so holding onto a `RegistryKey` inside Lua may lead to it never being dropped,
37+
/// and it if it is not droped, [`Context::expire_registry_values`] will never remove the value from
38+
/// the registry, leading to an uncollectable cycle. Instead of placing a `RegistryKey` into Lua
39+
/// and relying on it being automatically dropped, prefer APIs which the Lua garbage collector
40+
/// understands, such as [`UserData::set_user_value`] / [`UserData::get_user_value`] for UserData
41+
/// types and [`Function::bind`] for callbacks.
3842
///
3943
/// [`Context::registry_value`]: struct.Context.html#method.registry_value
4044
/// [`Context::remove_registry_value`]: struct.Context.html#method.remove_registry_value
4145
/// [`Context::expire_registry_values`]: struct.Context.html#method.expire_registry_values
42-
/// [`Context::scope`]: struct.Context.html#method.scope
46+
/// [`Function::bind`]: struct.Function.html#method.bind
4347
/// [`UserData::set_user_value`]: struct.UserData.html#method.set_user_value
4448
/// [`UserData::get_user_value`]: struct.UserData.html#method.get_user_value
4549
pub struct RegistryKey {

0 commit comments

Comments
 (0)