diff --git a/crates/luaffi/src/future.rs b/crates/luaffi/src/future.rs index 7a24646..284e33d 100644 --- a/crates/luaffi/src/future.rs +++ b/crates/luaffi/src/future.rs @@ -70,6 +70,40 @@ enum State { Complete, } +unsafe impl + 'static> Type for lua_future { + fn name() -> impl Display { + display!("__future_{:x}", type_id::()) + } + + fn ty() -> TypeType { + TypeType::Aggregate + } + + fn cdecl(name: impl Display) -> impl Display { + display!("struct {} {name}", Self::name()) + } + + fn build(s: &mut TypeBuilder) { + s.cdef::().metatype::(); + } +} + +unsafe impl + 'static> Cdef for lua_future { + fn build(s: &mut CdefBuilder) { + s.field_opaque(mem::offset_of!(Self, take)) // opaque .sig, .poll and .state + .field::::Into>>("__take") + .field::>("__drop"); + } +} + +unsafe impl + 'static> Metatype for lua_future { + type Target = Self; + + fn build(s: &mut MetatypeBuilder) { + s.metatable_raw("gc", luaify!(|self| self.__drop())); + } +} + impl> lua_future { pub fn new(fut: F) -> Self { Self { @@ -131,40 +165,6 @@ impl Future for lua_pollable { } } -unsafe impl + 'static> Type for lua_future { - fn name() -> impl Display { - display!("future__{:x}", type_id::()) - } - - fn ty() -> TypeType { - TypeType::Aggregate - } - - fn cdecl(name: impl Display) -> impl Display { - display!("struct {} {name}", Self::name()) - } - - fn build(s: &mut TypeBuilder) { - s.cdef::().metatype::(); - } -} - -unsafe impl + 'static> Cdef for lua_future { - fn build(s: &mut CdefBuilder) { - s.field_opaque(mem::offset_of!(Self, take)) // opaque .sig, .poll and .state - .field::::Into>>("__take") - .field::>("__drop"); - } -} - -unsafe impl + 'static> Metatype for lua_future { - type Target = Self; - - fn build(s: &mut MetatypeBuilder) { - s.metatable_raw("gc", luaify!(|self| self.__drop())); - } -} - unsafe impl + 'static> IntoFfi for lua_future { type Into = lua_future; diff --git a/crates/luaffi/src/option.rs b/crates/luaffi/src/option.rs index 672e327..56865c0 100644 --- a/crates/luaffi/src/option.rs +++ b/crates/luaffi/src/option.rs @@ -1,5 +1,5 @@ use crate::{ - __internal::{disp, display}, + __internal::{disp, display, type_id}, Cdef, CdefBuilder, IntoFfi, KEEP_FN, Type, TypeBuilder, TypeType, }; use std::{ffi::c_int, fmt::Display}; @@ -11,9 +11,9 @@ pub enum lua_option { Some(T), // __tag = 1 } -unsafe impl Type for lua_option { +unsafe impl Type for lua_option { fn name() -> impl Display { - display!("option__{}", T::name()) + display!("__option_{:x}", type_id::()) } fn ty() -> TypeType { @@ -29,14 +29,14 @@ unsafe impl Type for lua_option { } } -unsafe impl Cdef for lua_option { +unsafe impl Cdef for lua_option { fn build(b: &mut CdefBuilder) { b.field::("__tag"); (T::ty() != TypeType::Void).then(|| b.field::("__value")); } } -unsafe impl IntoFfi for Option { +unsafe impl> IntoFfi for Option { type Into = lua_option; fn convert(self) -> Self::Into { diff --git a/crates/luaffi/src/result.rs b/crates/luaffi/src/result.rs index c747673..5d6e01f 100644 --- a/crates/luaffi/src/result.rs +++ b/crates/luaffi/src/result.rs @@ -1,5 +1,5 @@ use crate::{ - __internal::{disp, display}, + __internal::{disp, display, type_id}, Cdef, CdefBuilder, IntoFfi, KEEP_FN, Type, TypeBuilder, TypeType, string::{DROP_BUFFER_FN, lua_buffer}, }; @@ -12,9 +12,9 @@ pub enum lua_result { Ok(T), // __tag = 1 } -unsafe impl Type for lua_result { +unsafe impl Type for lua_result { fn name() -> impl Display { - display!("result__{}", T::name()) + display!("__result_{:x}", type_id::()) } fn ty() -> TypeType { @@ -30,7 +30,7 @@ unsafe impl Type for lua_result { } } -unsafe impl Cdef for lua_result { +unsafe impl Cdef for lua_result { fn build(b: &mut CdefBuilder) { b.field::("__tag").inner_union(|b| { (T::ty() != TypeType::Void).then(|| b.field::("__value")); @@ -39,7 +39,7 @@ unsafe impl Cdef for lua_result { } } -unsafe impl IntoFfi for Result { +unsafe impl, E: Display> IntoFfi for Result { type Into = lua_result; fn convert(self) -> Self::Into {