Skip to content

Commit

Permalink
Merge pull request #952 from Yarwin/use-callable-custom-info-2
Browse files Browse the repository at this point in the history
Use `GDExtensionCallableCustomInfo2` instead of deprecated `GDExtensionCallableCustomInfo`.
  • Loading branch information
Bromeon authored Nov 23, 2024
2 parents 4c7db48 + b9d5318 commit f8a0939
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/other/deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ allow = [
"BSD-3-Clause",
"ISC",
"Unicode-DFS-2016",
"Unicode-3.0", # unicode-ident
"MPL-2.0",
#"Apache-2.0 WITH LLVM-exception",
]
Expand Down
26 changes: 20 additions & 6 deletions godot-core/src/builtin/callable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ use crate::obj::{Gd, GodotClass, InstanceId};
use std::{fmt, ptr};
use sys::{ffi_methods, GodotFfi};

#[cfg(all(since_api = "4.2", before_api = "4.3"))]
type CallableCustomInfo = sys::GDExtensionCallableCustomInfo;
#[cfg(since_api = "4.3")]
type CallableCustomInfo = sys::GDExtensionCallableCustomInfo2;

/// A `Callable` represents a function in Godot.
///
/// Usually a callable is a reference to an `Object` and a method name, this is a standard callable. But can
Expand Down Expand Up @@ -61,8 +66,8 @@ impl Callable {
}

#[cfg(since_api = "4.2")]
fn default_callable_custom_info() -> sys::GDExtensionCallableCustomInfo {
sys::GDExtensionCallableCustomInfo {
fn default_callable_custom_info() -> CallableCustomInfo {
CallableCustomInfo {
callable_userdata: ptr::null_mut(),
token: ptr::null_mut(),
object_id: 0,
Expand All @@ -74,6 +79,8 @@ impl Callable {
// Op < is only used in niche scenarios and default is usually good enough, see https://github.com/godotengine/godot/issues/81901.
less_than_func: None,
to_string_func: None,
#[cfg(since_api = "4.3")]
get_argument_count_func: None,
}
}

Expand Down Expand Up @@ -105,7 +112,7 @@ impl Callable {
},
};

let info = sys::GDExtensionCallableCustomInfo {
let info = CallableCustomInfo {
callable_userdata: Box::into_raw(Box::new(userdata)) as *mut std::ffi::c_void,
call_func: Some(rust_callable_call_fn::<F>),
free_func: Some(rust_callable_destroy::<FnWrapper<F>>),
Expand All @@ -126,7 +133,7 @@ impl Callable {
// - a type-erased workaround for PartialEq supertrait (which has a `Self` type parameter and thus is not object-safe)
let userdata = CallableUserdata { inner: callable };

let info = sys::GDExtensionCallableCustomInfo {
let info = CallableCustomInfo {
callable_userdata: Box::into_raw(Box::new(userdata)) as *mut std::ffi::c_void,
call_func: Some(rust_callable_call_custom::<C>),
free_func: Some(rust_callable_destroy::<C>),
Expand All @@ -140,11 +147,18 @@ impl Callable {
}

#[cfg(since_api = "4.2")]
fn from_custom_info(mut info: sys::GDExtensionCallableCustomInfo) -> Callable {
fn from_custom_info(mut info: CallableCustomInfo) -> Callable {
// SAFETY: callable_custom_create() is a valid way of creating callables.
unsafe {
Callable::new_with_uninit(|type_ptr| {
sys::interface_fn!(callable_custom_create)(type_ptr, ptr::addr_of_mut!(info))
#[cfg(before_api = "4.3")]
{
sys::interface_fn!(callable_custom_create)(type_ptr, ptr::addr_of_mut!(info))
}
#[cfg(since_api = "4.3")]
{
sys::interface_fn!(callable_custom_create2)(type_ptr, ptr::addr_of_mut!(info))
}
})
}
}
Expand Down

0 comments on commit f8a0939

Please sign in to comment.