Rename ToFfi to IntoFfi

This commit is contained in:
2025-06-24 11:42:11 +10:00
parent cadf0a8551
commit 45db380466
6 changed files with 176 additions and 193 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
__internal::{display, type_id},
Cdef, CdefBuilder, FfiReturnConvention, Metatype, MetatypeBuilder, ToFfi, Type, TypeBuilder,
Cdef, CdefBuilder, FfiReturnConvention, IntoFfi, Metatype, MetatypeBuilder, Type, TypeBuilder,
UnsafeExternCFn,
};
use luaify::luaify;
@@ -21,7 +21,7 @@ const SIGNATURE: Signature = Signature::from_ne_bytes(*b"\x00lb_poll");
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct lua_future<F: Future<Output: ToFfi>> {
pub struct lua_future<F: Future<Output: IntoFfi>> {
//
// SAFETY: LuaJIT guarantees that cdata payloads, which are GC-managed, are never relocated
// (i.e. pinned). We can safely assume that we are pinned and poll the future inside this
@@ -43,7 +43,7 @@ pub struct lua_future<F: Future<Output: ToFfi>> {
sig: Signature,
poll: fn(Pin<&mut Self>, cx: &mut Context) -> Poll<()>,
state: State<F>,
take: unsafe extern "C" fn(&mut Self) -> <F::Output as ToFfi>::To,
take: unsafe extern "C" fn(&mut Self) -> <F::Output as IntoFfi>::To,
drop: unsafe extern "C" fn(&mut Self),
}
@@ -70,7 +70,7 @@ enum State<F: Future> {
Complete,
}
impl<F: Future<Output: ToFfi>> lua_future<F> {
impl<F: Future<Output: IntoFfi>> lua_future<F> {
pub fn new(fut: F) -> Self {
Self {
sig: SIGNATURE,
@@ -94,7 +94,7 @@ impl<F: Future<Output: ToFfi>> lua_future<F> {
}
}
unsafe extern "C" fn take(&mut self) -> <F::Output as ToFfi>::To {
unsafe extern "C" fn take(&mut self) -> <F::Output as IntoFfi>::To {
// `fut:__take()` returns the fulfilled value by-value (not by out-param) because if we
// preallocate a cdata for the out-param and the thread for some reason gets dropped and
// never resumed, the GC could call the destructor on an uninitialised cdata.
@@ -131,7 +131,7 @@ impl Future for lua_pollable {
}
}
unsafe impl<F: Future<Output: ToFfi> + 'static> Type for lua_future<F> {
unsafe impl<F: Future<Output: IntoFfi> + 'static> Type for lua_future<F> {
fn name() -> impl Display {
display!("future__{:x}", type_id::<F>())
}
@@ -145,15 +145,15 @@ unsafe impl<F: Future<Output: ToFfi> + 'static> Type for lua_future<F> {
}
}
unsafe impl<F: Future<Output: ToFfi> + 'static> Cdef for lua_future<F> {
unsafe impl<F: Future<Output: IntoFfi> + 'static> Cdef for lua_future<F> {
fn build(s: &mut CdefBuilder) {
s.field_opaque(mem::offset_of!(Self, take)) // opaque .sig, .poll and .state
.field::<UnsafeExternCFn<(&mut Self,), <F::Output as ToFfi>::To>>("__take")
.field::<UnsafeExternCFn<(&mut Self,), <F::Output as IntoFfi>::To>>("__take")
.field::<UnsafeExternCFn<(&mut Self,), ()>>("__drop");
}
}
unsafe impl<F: Future<Output: ToFfi> + 'static> Metatype for lua_future<F> {
unsafe impl<F: Future<Output: IntoFfi> + 'static> Metatype for lua_future<F> {
type Target = Self;
fn build(s: &mut MetatypeBuilder) {
@@ -161,7 +161,7 @@ unsafe impl<F: Future<Output: ToFfi> + 'static> Metatype for lua_future<F> {
}
}
unsafe impl<F: Future<Output: ToFfi> + 'static> ToFfi for lua_future<F> {
unsafe impl<F: Future<Output: IntoFfi> + 'static> IntoFfi for lua_future<F> {
type To = lua_future<F>;
fn convert(self) -> Self::To {
@@ -179,12 +179,12 @@ unsafe impl<F: Future<Output: ToFfi> + 'static> ToFfi for lua_future<F> {
// `coroutine.yield` is cached as `yield` and `ffi.gc` as `gc` in locals (see lib.rs)
display!(
"yield({ret}); {ret} = gc({ret}, nil):__take(); {}",
<F::Output as ToFfi>::postlude(ret, FfiReturnConvention::ByValue)
<F::Output as IntoFfi>::postlude(ret, FfiReturnConvention::ByValue)
)
}
}
impl<F: IntoFuture<Output: ToFfi>> From<F> for lua_future<F::IntoFuture> {
impl<F: IntoFuture<Output: IntoFfi>> From<F> for lua_future<F::IntoFuture> {
fn from(value: F) -> Self {
Self::new(value.into_future())
}