Skip to content

Commit

Permalink
Merge pull request #421 from lilizoey/feature/godot-compat
Browse files Browse the repository at this point in the history
Create a new consolidated API to pass types across the ffi-boundary
Bromeon authored Oct 13, 2023
2 parents 7594e9c + ffe241a commit 844f0f3
Showing 68 changed files with 2,107 additions and 1,256 deletions.
14 changes: 1 addition & 13 deletions godot-codegen/src/central_generator.rs
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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)]
@@ -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; .. }
}
}
}

3 changes: 2 additions & 1 deletion godot-codegen/src/class_generator.rs
Original file line number Diff line number Diff line change
@@ -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,
23 changes: 12 additions & 11 deletions godot-codegen/src/util.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 8 additions & 0 deletions godot-core/src/builtin/aabb.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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`.
Loading

0 comments on commit 844f0f3

Please sign in to comment.