Skip to content

Commit 5dc6c32

Browse files
committed
Prepare for 2024 edition
1 parent da08f72 commit 5dc6c32

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

mlua-sys/src/luau/lauxlib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::os::raw::{c_char, c_float, c_int, c_void};
44
use std::ptr;
55

6-
use super::lua::{self, LUA_REGISTRYINDEX, lua_CFunction, lua_Number, lua_State, lua_Unsigned};
6+
use super::lua::{self, lua_CFunction, lua_Number, lua_State, lua_Unsigned, LUA_REGISTRYINDEX};
77

88
#[repr(C)]
99
pub struct luaL_Reg {

src/function.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ use crate::value::Value;
1414

1515
#[cfg(feature = "async")]
1616
use {
17+
crate::thread::AsyncThread,
1718
crate::traits::LuaNativeAsyncFn,
1819
crate::types::AsyncCallback,
1920
std::future::{self, Future},
21+
std::pin::Pin,
22+
std::task::{Context, Poll},
2023
};
2124

2225
/// Handle to an internal Lua function.
@@ -128,7 +131,8 @@ impl Function {
128131
/// Returns a future that, when polled, calls `self`, passing `args` as function arguments,
129132
/// and drives the execution.
130133
///
131-
/// Internally it wraps the function to an [`AsyncThread`].
134+
/// Internally it wraps the function to an [`AsyncThread`]. The returned type implements
135+
/// `Future<Output = Result<R>>` and can be awaited.
132136
///
133137
/// Requires `feature = "async"`
134138
///
@@ -155,19 +159,18 @@ impl Function {
155159
/// [`AsyncThread`]: crate::AsyncThread
156160
#[cfg(feature = "async")]
157161
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
158-
pub fn call_async<R>(&self, args: impl IntoLuaMulti) -> impl Future<Output = Result<R>>
162+
pub fn call_async<R>(&self, args: impl IntoLuaMulti) -> AsyncCallFuture<R>
159163
where
160164
R: FromLuaMulti,
161165
{
162166
let lua = self.0.lua.lock();
163-
let thread_res = unsafe {
167+
AsyncCallFuture(unsafe {
164168
lua.create_recycled_thread(self).and_then(|th| {
165169
let mut th = th.into_async(args)?;
166170
th.set_recyclable(true);
167171
Ok(th)
168172
})
169-
};
170-
async move { thread_res?.await }
173+
})
171174
}
172175

173176
/// Returns a function that, when called, calls `self`, passing `args` as the first set of
@@ -644,6 +647,26 @@ impl LuaType for Function {
644647
const TYPE_ID: c_int = ffi::LUA_TFUNCTION;
645648
}
646649

650+
#[cfg(feature = "async")]
651+
pub struct AsyncCallFuture<R: FromLuaMulti>(Result<AsyncThread<R>>);
652+
653+
#[cfg(feature = "async")]
654+
impl<R: FromLuaMulti> Future for AsyncCallFuture<R> {
655+
type Output = Result<R>;
656+
657+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
658+
// Safety: We're not moving any pinned data
659+
let this = unsafe { self.get_unchecked_mut() };
660+
match &mut this.0 {
661+
Ok(thread) => {
662+
let pinned_thread = unsafe { Pin::new_unchecked(thread) };
663+
pinned_thread.poll(cx)
664+
}
665+
Err(err) => Poll::Ready(Err(err.clone())),
666+
}
667+
}
668+
}
669+
647670
#[cfg(test)]
648671
mod assertions {
649672
use super::*;
@@ -652,4 +675,7 @@ mod assertions {
652675
static_assertions::assert_not_impl_any!(Function: Send);
653676
#[cfg(feature = "send")]
654677
static_assertions::assert_impl_all!(Function: Send, Sync);
678+
679+
#[cfg(all(feature = "async", feature = "send"))]
680+
static_assertions::assert_impl_all!(AsyncCallFuture<()>: Send);
655681
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#![cfg_attr(docsrs, feature(doc_cfg))]
6868
#![cfg_attr(not(send), allow(clippy::arc_with_non_send_sync))]
6969
#![allow(clippy::ptr_eq)]
70+
#![allow(unsafe_op_in_unsafe_fn)]
7071

7172
#[macro_use]
7273
mod macros;

src/util/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ pub(crate) unsafe fn init_error_registry(state: *mut ffi::lua_State) -> Result<(
315315
let _ = write!(&mut (*err_buf), "{error}");
316316
Ok(err_buf)
317317
}
318-
Some(WrappedFailure::Panic(Some(ref panic))) => {
318+
Some(WrappedFailure::Panic(Some(panic))) => {
319319
let err_buf_key = &ERROR_PRINT_BUFFER_KEY as *const u8 as *const c_void;
320320
ffi::lua_rawgetp(state, ffi::LUA_REGISTRYINDEX, err_buf_key);
321321
let err_buf = ffi::lua_touserdata(state, -1) as *mut String;

tests/tests.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -944,9 +944,11 @@ fn test_rust_function() -> Result<()> {
944944
fn test_c_function() -> Result<()> {
945945
let lua = Lua::new();
946946

947-
unsafe extern "C-unwind" fn c_function(state: *mut mlua::lua_State) -> std::os::raw::c_int {
948-
ffi::lua_pushboolean(state, 1);
949-
ffi::lua_setglobal(state, b"c_function\0" as *const _ as *const _);
947+
extern "C-unwind" fn c_function(state: *mut mlua::lua_State) -> std::os::raw::c_int {
948+
unsafe {
949+
ffi::lua_pushboolean(state, 1);
950+
ffi::lua_setglobal(state, b"c_function\0" as *const _ as *const _);
951+
}
950952
0
951953
}
952954

tests/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn test_thread_reset() -> Result<()> {
164164
let result = thread.resume::<()>(());
165165
assert!(
166166
matches!(result, Err(Error::CallbackError{ ref cause, ..})
167-
if matches!(cause.as_ref(), Error::RuntimeError(ref err)
167+
if matches!(cause.as_ref(), Error::RuntimeError(err)
168168
if err == "cannot reset a running thread")
169169
),
170170
"unexpected result: {result:?}",

0 commit comments

Comments
 (0)