summaryrefslogtreecommitdiff
path: root/src/types.rs
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2020-04-17 22:38:01 +0100
committerAlex Orlenko <zxteam@protonmail.com>2020-04-17 22:39:50 +0100
commit47e8a80c1cbc6b76a341029df5555aa098ce4816 (patch)
tree9ebadb5f90b28e10647e4ac68e3d1fd2965a8791 /src/types.rs
parent1a788c48f1612ac4a803f644f7285688ed97864a (diff)
downloadmlua-47e8a80c1cbc6b76a341029df5555aa098ce4816.zip
v0.3.0-alpha.1 with async support
Squashed commit of the async branch.
Diffstat (limited to 'src/types.rs')
-rw-r--r--src/types.rs28
1 files changed, 16 insertions, 12 deletions
diff --git a/src/types.rs b/src/types.rs
index e88d0c1..38065bc 100644
--- a/src/types.rs
+++ b/src/types.rs
@@ -1,7 +1,11 @@
+use std::cell::RefCell;
use std::os::raw::{c_int, c_void};
-use std::sync::{Arc, Mutex};
+use std::rc::Rc;
use std::{fmt, mem, ptr};
+#[cfg(feature = "async")]
+use futures_core::future::LocalBoxFuture;
+
use crate::error::Result;
use crate::ffi;
use crate::lua::Lua;
@@ -20,29 +24,28 @@ pub struct LightUserData(pub *mut c_void);
pub(crate) type Callback<'lua, 'a> =
Box<dyn Fn(&'lua Lua, MultiValue<'lua>) -> Result<MultiValue<'lua>> + 'a>;
+#[cfg(feature = "async")]
+pub(crate) type AsyncCallback<'lua, 'a> =
+ Box<dyn Fn(&'lua Lua, MultiValue<'lua>) -> LocalBoxFuture<'lua, Result<MultiValue<'lua>>> + 'a>;
+
/// An auto generated key into the Lua registry.
///
-/// This is a handle to a value stored inside the Lua registry. It is not directly usable like the
-/// `Table` or `Function` handle types, but since it doesn't hold a reference to a parent Lua and is
-/// Send + Sync + 'static, it is much more flexible and can be used in many situations where it is
-/// impossible to directly store a normal handle type. It is not automatically garbage collected on
-/// Drop, but it can be removed with [`Lua::remove_registry_value`], and instances not manually
-/// removed can be garbage collected with [`Lua::expire_registry_values`].
+/// This is a handle to a value stored inside the Lua registry. It is not automatically
+/// garbage collected on Drop, but it can be removed with [`Lua::remove_registry_value`],
+/// and instances not manually removed can be garbage collected with [`Lua::expire_registry_values`].
///
/// Be warned, If you place this into Lua via a `UserData` type or a rust callback, it is *very
/// easy* to accidentally cause reference cycles that the Lua garbage collector cannot resolve.
/// Instead of placing a `RegistryKey` into a `UserData` type, prefer instead to use
-/// [`UserData::set_user_value`] / [`UserData::get_user_value`], and instead of moving a RegistryKey
-/// into a callback, prefer [`Lua::scope`].
+/// [`UserData::set_user_value`] / [`UserData::get_user_value`].
///
/// [`Lua::remove_registry_value`]: struct.Lua.html#method.remove_registry_value
/// [`Lua::expire_registry_values`]: struct.Lua.html#method.expire_registry_values
-/// [`Lua::scope`]: struct.Lua.html#method.scope
/// [`UserData::set_user_value`]: struct.UserData.html#method.set_user_value
/// [`UserData::get_user_value`]: struct.UserData.html#method.get_user_value
pub struct RegistryKey {
pub(crate) registry_id: c_int,
- pub(crate) unref_list: Arc<Mutex<Option<Vec<c_int>>>>,
+ pub(crate) unref_list: Rc<RefCell<Option<Vec<c_int>>>>,
}
impl fmt::Debug for RegistryKey {
@@ -53,7 +56,8 @@ impl fmt::Debug for RegistryKey {
impl Drop for RegistryKey {
fn drop(&mut self) {
- if let Some(list) = mlua_expect!(self.unref_list.lock(), "unref_list poisoned").as_mut() {
+ let mut unref_list = mlua_expect!(self.unref_list.try_borrow_mut(), "unref list borrowed");
+ if let Some(list) = unref_list.as_mut() {
list.push(self.registry_id);
}
}