Skip to content

Commit afc16aa

Browse files
committed
Bring TerminateInfo and InitializeInfo back to public
The types are used as arguments to custom init/terminate callbacks, and aren't entirely internal.
1 parent dd1629f commit afc16aa

File tree

4 files changed

+105
-108
lines changed

4 files changed

+105
-108
lines changed

gdnative-core/src/lib.rs

Lines changed: 98 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@
2020
//! [thread-safety]: https://docs.godotengine.org/en/stable/tutorials/threads/thread_safe_apis.html
2121
2222
#![deny(clippy::missing_inline_in_public_items)]
23-
#![allow(
24-
clippy::transmute_ptr_to_ptr,
25-
clippy::missing_safety_doc,
26-
clippy::if_then_panic
27-
)]
23+
#![allow(clippy::transmute_ptr_to_ptr, clippy::missing_safety_doc, clippy::if_then_panic)]
2824
#![cfg_attr(feature = "gd_test", allow(clippy::blacklisted_name))]
2925

3026
#[doc(hidden)]
@@ -51,3 +47,100 @@ pub mod object;
5147
/// Internal low-level API for use by macros and generated bindings. Not a part of the public API.
5248
#[doc(hidden)]
5349
pub mod private;
50+
51+
use core_types::GodotString;
52+
53+
/// Context for the [`godot_gdnative_terminate`] callback.
54+
pub struct TerminateInfo {
55+
in_editor: bool,
56+
}
57+
58+
impl TerminateInfo {
59+
#[inline]
60+
#[doc(hidden)] // avoids clippy warning: unsafe function's docs miss `# Safety` section
61+
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_terminate_options) -> Self {
62+
assert!(!options.is_null(), "options were NULL");
63+
64+
let crate::sys::godot_gdnative_terminate_options { in_editor } = *options;
65+
66+
Self { in_editor }
67+
}
68+
69+
/// Returns `true` if the library is loaded in the Godot Editor.
70+
#[inline]
71+
pub fn in_editor(&self) -> bool {
72+
self.in_editor
73+
}
74+
}
75+
76+
/// Context for the [`godot_gdnative_init`] callback.
77+
pub struct InitializeInfo {
78+
in_editor: bool,
79+
active_library_path: GodotString,
80+
options: *mut crate::sys::godot_gdnative_init_options,
81+
}
82+
83+
impl InitializeInfo {
84+
/// Returns true if the library is loaded in the Godot Editor.
85+
#[inline]
86+
pub fn in_editor(&self) -> bool {
87+
self.in_editor
88+
}
89+
90+
/// Returns a path to the library relative to the project.
91+
///
92+
/// Example: `res://../../target/debug/libhello_world.dylib`
93+
#[inline]
94+
pub fn active_library_path(&self) -> &GodotString {
95+
&self.active_library_path
96+
}
97+
98+
/// Internal interface.
99+
///
100+
/// # Safety
101+
///
102+
/// Will `panic!()` if options is NULL, UB if invalid.
103+
#[inline]
104+
#[doc(hidden)]
105+
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_init_options) -> Self {
106+
assert!(!options.is_null(), "options were NULL");
107+
let crate::sys::godot_gdnative_init_options {
108+
in_editor,
109+
active_library_path,
110+
..
111+
} = *options;
112+
113+
let active_library_path = GodotString::clone_from_sys(*active_library_path);
114+
115+
Self {
116+
in_editor,
117+
active_library_path,
118+
options,
119+
}
120+
}
121+
122+
#[inline]
123+
pub fn report_loading_error<T>(&self, message: T)
124+
where
125+
T: std::fmt::Display,
126+
{
127+
let crate::sys::godot_gdnative_init_options {
128+
report_loading_error,
129+
gd_native_library,
130+
..
131+
} = unsafe { *self.options };
132+
133+
if let Some(report_loading_error_fn) = report_loading_error {
134+
// Add the trailing zero and convert Display => String
135+
let message = format!("{}\0", message);
136+
137+
// Convert to FFI compatible string
138+
let message = std::ffi::CStr::from_bytes_with_nul(message.as_bytes())
139+
.expect("message should not have a NULL");
140+
141+
unsafe {
142+
report_loading_error_fn(gd_native_library, message.as_ptr());
143+
}
144+
}
145+
}
146+
}

gdnative-core/src/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#[macro_export]
1717
macro_rules! godot_gdnative_init {
1818
() => {
19-
fn godot_gdnative_init_empty(_options: &$crate::private::InitializeInfo) {}
19+
fn godot_gdnative_init_empty(_options: &$crate::InitializeInfo) {}
2020
$crate::godot_gdnative_init!(godot_gdnative_init_empty);
2121
};
2222
(_ as $fn_name:ident) => {
23-
fn godot_gdnative_init_empty(_options: &$crate::private::InitializeInfo) {}
23+
fn godot_gdnative_init_empty(_options: &$crate::InitializeInfo) {}
2424
$crate::godot_gdnative_init!(godot_gdnative_init_empty as $fn_name);
2525
};
2626
($callback:ident) => {
@@ -38,7 +38,7 @@ macro_rules! godot_gdnative_init {
3838
}
3939

4040
let __result = ::std::panic::catch_unwind(|| {
41-
let callback_options = $crate::private::InitializeInfo::new(options);
41+
let callback_options = $crate::InitializeInfo::new(options);
4242
$callback(&callback_options)
4343
});
4444
if __result.is_err() {
@@ -64,14 +64,14 @@ macro_rules! godot_gdnative_init {
6464
#[macro_export]
6565
macro_rules! godot_gdnative_terminate {
6666
() => {
67-
fn godot_gdnative_terminate_empty(_term_info: &$crate::private::TerminateInfo) {}
67+
fn godot_gdnative_terminate_empty(_term_info: &$crate::TerminateInfo) {}
6868
$crate::godot_gdnative_terminate!(godot_gdnative_terminate_empty);
6969
};
7070
($callback:ident) => {
7171
$crate::godot_gdnative_terminate!($callback as godot_gdnative_terminate);
7272
};
7373
(_ as $fn_name:ident) => {
74-
fn godot_gdnative_terminate_empty(_term_info: &$crate::private::TerminateInfo) {}
74+
fn godot_gdnative_terminate_empty(_term_info: &$crate::TerminateInfo) {}
7575
$crate::godot_gdnative_terminate!(godot_gdnative_terminate_empty as $fn_name);
7676
};
7777
($callback:ident as $fn_name:ident) => {
@@ -86,7 +86,7 @@ macro_rules! godot_gdnative_terminate {
8686
}
8787

8888
let __result = ::std::panic::catch_unwind(|| {
89-
let term_info = $crate::private::TerminateInfo::new(options);
89+
let term_info = $crate::TerminateInfo::new(options);
9090
$callback(&term_info)
9191
});
9292
if __result.is_err() {

gdnative-core/src/private.rs

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::ffi::CString;
22

3-
use crate::core_types::GodotString;
43
use crate::sys;
54

65
// ----------------------------------------------------------------------------------------------------------------------------------------------
@@ -234,98 +233,3 @@ make_method_table!(struct NativeScriptMethodTable for NativeScript {
234233
set_library,
235234
new,
236235
});
237-
238-
// ----------------------------------------------------------------------------------------------------------------------------------------------
239-
// Helper structs for init/terminate
240-
241-
pub struct TerminateInfo {
242-
in_editor: bool,
243-
}
244-
245-
impl TerminateInfo {
246-
#[inline]
247-
#[doc(hidden)] // avoids clippy warning: unsafe function's docs miss `# Safety` section
248-
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_terminate_options) -> Self {
249-
assert!(!options.is_null(), "options were NULL");
250-
251-
let crate::sys::godot_gdnative_terminate_options { in_editor } = *options;
252-
253-
Self { in_editor }
254-
}
255-
256-
/// Returns `true` if the library is loaded in the Godot Editor.
257-
#[inline]
258-
pub fn in_editor(&self) -> bool {
259-
self.in_editor
260-
}
261-
}
262-
263-
pub struct InitializeInfo {
264-
in_editor: bool,
265-
active_library_path: GodotString,
266-
options: *mut crate::sys::godot_gdnative_init_options,
267-
}
268-
269-
impl InitializeInfo {
270-
/// Returns true if the library is loaded in the Godot Editor.
271-
#[inline]
272-
pub fn in_editor(&self) -> bool {
273-
self.in_editor
274-
}
275-
276-
/// Returns a path to the library relative to the project.
277-
///
278-
/// Example: `res://../../target/debug/libhello_world.dylib`
279-
#[inline]
280-
pub fn active_library_path(&self) -> &GodotString {
281-
&self.active_library_path
282-
}
283-
284-
/// # Safety
285-
///
286-
/// Will `panic!()` if options is NULL or invalid.
287-
#[inline]
288-
#[doc(hidden)]
289-
pub unsafe fn new(options: *mut crate::sys::godot_gdnative_init_options) -> Self {
290-
assert!(!options.is_null(), "options were NULL");
291-
let crate::sys::godot_gdnative_init_options {
292-
in_editor,
293-
active_library_path,
294-
..
295-
} = *options;
296-
297-
let active_library_path =
298-
crate::core_types::GodotString::clone_from_sys(*active_library_path);
299-
300-
Self {
301-
in_editor,
302-
active_library_path,
303-
options,
304-
}
305-
}
306-
307-
#[inline]
308-
pub fn report_loading_error<T>(&self, message: T)
309-
where
310-
T: std::fmt::Display,
311-
{
312-
let crate::sys::godot_gdnative_init_options {
313-
report_loading_error,
314-
gd_native_library,
315-
..
316-
} = unsafe { *self.options };
317-
318-
if let Some(report_loading_error_fn) = report_loading_error {
319-
// Add the trailing zero and convert Display => String
320-
let message = format!("{}\0", message);
321-
322-
// Convert to FFI compatible string
323-
let message = std::ffi::CStr::from_bytes_with_nul(message.as_bytes())
324-
.expect("message should not have a NULL");
325-
326-
unsafe {
327-
report_loading_error_fn(gd_native_library, message.as_ptr());
328-
}
329-
}
330-
}
331-
}

gdnative/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
// Items, which are #[doc(hidden)] in their original crate and re-exported with a wildcard, lose
5959
// their hidden status. Re-exporting them manually and hiding the wildcard solves this.
6060
#[doc(inline)]
61-
pub use gdnative_core::{core_types, log, nativescript, object};
61+
pub use gdnative_core::{core_types, log, nativescript, object, InitializeInfo, TerminateInfo};
6262

6363
/// Collection of declarative `godot_*` macros, mostly for GDNative registration and output.
6464
pub mod macros {

0 commit comments

Comments
 (0)