Skip to content

Create a new consolidated API to pass types across the ffi-boundary #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions godot-codegen/src/central_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ fn make_sys_code(central_items: &CentralItems) -> TokenStream {
let [opaque_32bit, opaque_64bit] = opaque_types;

quote! {
use crate::{ffi_methods, GDExtensionTypePtr, GDExtensionVariantOperator, GDExtensionVariantType, GodotFfi};
use crate::{GDExtensionVariantOperator, GDExtensionVariantType};

#[cfg(target_pointer_width = "32")]
pub mod types {
Expand Down Expand Up @@ -443,12 +443,6 @@ fn make_sys_code(central_items: &CentralItems) -> TokenStream {
}
}

// SAFETY:
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
unsafe impl GodotFfi for VariantType {
ffi_methods! { type GDExtensionTypePtr = *mut Self; .. }
}

// ----------------------------------------------------------------------------------------------------------------------------------------------

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
Expand All @@ -475,12 +469,6 @@ fn make_sys_code(central_items: &CentralItems) -> TokenStream {
self as _
}
}

// SAFETY:
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
unsafe impl GodotFfi for VariantOperator {
ffi_methods! { type GDExtensionTypePtr = *mut Self; .. }
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion godot-codegen/src/class_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,8 @@ fn make_special_builtin_methods(class_name: &TyName, _ctx: &Context) -> TokenStr
if class_name.godot_ty == "Array" {
quote! {
pub fn from_outer_typed<T>(outer: &Array<T>) -> Self
where T: crate::builtin::meta::VariantMetadata
where
T: crate::builtin::meta::GodotType
{
Self {
_outer_lifetime: std::marker::PhantomData,
Expand Down
23 changes: 12 additions & 11 deletions godot-codegen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,21 +334,22 @@ pub fn make_enum_definition(enum_: &Enum) -> TokenStream {
}
#index_enum_impl

impl sys::GodotFuncMarshal for #enum_name {
type Via = i64;
type FromViaError = sys::PrimitiveConversionError<i64, i32>;
type IntoViaError = std::convert::Infallible;

fn try_from_via(via: Self::Via) -> std::result::Result<Self, Self::FromViaError> {
let err = sys::PrimitiveConversionError::new(via);
let ord = i32::try_from(via).map_err(|_| err)?;
<Self as crate::obj::EngineEnum>::try_from_ord(ord).ok_or(err)
impl crate::builtin::meta::GodotConvert for #enum_name {
type Via = i32;
}

fn try_into_via(self) -> std::result::Result<Self::Via, Self::IntoViaError> {
Ok(<Self as crate::obj::EngineEnum>::ord(self).into())
impl crate::builtin::meta::ToGodot for #enum_name {
fn to_godot(&self) -> Self::Via {
<Self as crate::obj::EngineEnum>::ord(*self)
}
}

impl crate::builtin::meta::FromGodot for #enum_name {
fn try_from_godot(via: Self::Via) -> Option<Self> {
<Self as crate::obj::EngineEnum>::try_from_ord(via)
}
}

#bitfield_ops
}
}
Expand Down
8 changes: 8 additions & 0 deletions godot-core/src/builtin/aabb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use sys::{ffi_methods, GodotFfi};
use crate::builtin::math::ApproxEq;
use crate::builtin::{real, Plane, Vector3, Vector3Axis};

use super::meta::impl_godot_as_self;

/// Axis-aligned bounding box in 3D space.
///
/// `Aabb` consists of a position, a size, and several utility functions. It is typically used for
Expand Down Expand Up @@ -396,9 +398,15 @@ impl std::fmt::Display for Aabb {
// SAFETY:
// This type is represented as `Self` in Godot, so `*mut Self` is sound.
unsafe impl GodotFfi for Aabb {
fn variant_type() -> sys::VariantType {
sys::VariantType::Aabb
}

ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. }
}

impl_godot_as_self!(Aabb);

impl ApproxEq for Aabb {
/// Returns `true` if the two `Aabb`s are approximately equal, by calling `is_equal_approx` on
/// `position` and `size`.
Expand Down
Loading