Skip to content

Commit 686b59d

Browse files
committed
Add sys::conv
Add `#[deny(unsafe_op_in_unsafe_fn)]` to `script_instance_info` Use `sys::conv` bools Final touches
1 parent 3e80962 commit 686b59d

File tree

11 files changed

+359
-232
lines changed

11 files changed

+359
-232
lines changed

godot-core/src/builtin/callable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ mod custom_callable {
432432
let a: &T = CallableUserdata::inner_from_raw(callable_userdata_a);
433433
let b: &T = CallableUserdata::inner_from_raw(callable_userdata_b);
434434

435-
(a == b) as sys::GDExtensionBool
435+
sys::conv::bool_to_sys(a == b)
436436
}
437437

438438
pub unsafe extern "C" fn rust_callable_to_string_display<T: fmt::Display>(
@@ -444,7 +444,7 @@ mod custom_callable {
444444
let s = crate::builtin::GString::from(c.to_string());
445445

446446
s.move_into_string_ptr(r_out);
447-
*r_is_valid = true as sys::GDExtensionBool;
447+
*r_is_valid = sys::conv::SYS_TRUE;
448448
}
449449

450450
pub unsafe extern "C" fn rust_callable_to_string_named<F>(
@@ -455,6 +455,6 @@ mod custom_callable {
455455
let w: &mut FnWrapper<F> = CallableUserdata::inner_from_raw(callable_userdata);
456456

457457
w.name.clone().move_into_string_ptr(r_out);
458-
*r_is_valid = true as sys::GDExtensionBool;
458+
*r_is_valid = sys::conv::SYS_TRUE;
459459
}
460460
}

godot-core/src/builtin/string/string_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl From<&'static std::ffi::CStr> for StringName {
317317
sys::interface_fn!(string_name_new_with_latin1_chars)(
318318
ptr,
319319
c_str.as_ptr(),
320-
true as sys::GDExtensionBool, // p_is_static
320+
sys::conv::SYS_TRUE, // p_is_static
321321
)
322322
})
323323
};

godot-core/src/meta/mod.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod traits;
1717
pub mod error;
1818
pub use class_name::ClassName;
1919
pub use godot_convert::{FromGodot, GodotConvert, ToGodot};
20+
use sys::conv::u32_to_usize;
2021
pub use traits::{ArrayElement, GodotType};
2122

2223
pub(crate) use crate::impl_godot_as_self;
@@ -312,43 +313,46 @@ impl MethodInfo {
312313
default_arguments,
313314
} = info;
314315

315-
// SAFETY: `name` and `return_value` were created from the appropriate method calls, and have not been freed before this.
316-
unsafe {
317-
let _name = StringName::from_owned_string_sys(name);
318-
PropertyInfo::free_owned_property_sys(return_value);
319-
}
316+
// SAFETY: `name` is a pointer that was returned from `StringName::into_owned_string_sys`, and has not been freed before this.
317+
let _name = unsafe { StringName::from_owned_string_sys(name) };
318+
319+
// SAFETY: `return_value` is a pointer that was returned from `PropertyInfo::into_owned_property_sys`, and has not been freed before
320+
// this.
321+
unsafe { PropertyInfo::free_owned_property_sys(return_value) };
322+
323+
// SAFETY:
324+
// - `from_raw_parts_mut`: `arguments` comes from `as_mut_ptr()` on a mutable slice of length `argument_count`, and no other
325+
// accesses to the pointer happens for the lifetime of the slice.
326+
// - `Box::from_raw`: The slice was returned from a call to `Box::leak`, and we have ownership of the value behind this pointer.
327+
let arguments = unsafe {
328+
let slice = std::slice::from_raw_parts_mut(arguments, u32_to_usize(argument_count));
320329

321-
// SAFETY: These pointers were both created from a call to `as_mut_ptr` on a slice. Additionally these pointer will not be accessed
322-
// again after this function call.
323-
let (arguments_slice, default_arguments_slice) = unsafe {
324-
(
325-
std::slice::from_raw_parts_mut(
326-
arguments,
327-
argument_count
328-
.try_into()
329-
.expect("gdext only supports targets where u32 <= usize"),
330-
),
331-
std::slice::from_raw_parts_mut(
332-
default_arguments,
333-
default_argument_count
334-
.try_into()
335-
.expect("gdext only supports targets where u32 <= usize"),
336-
),
337-
)
330+
Box::from_raw(slice)
338331
};
339332

340-
// SAFETY: We have exclusive ownership of these slices, and they were originally created from a call to `Box::leak`.
341-
let (_arguments, default_arguments) = unsafe {
342-
(
343-
Box::from_raw(arguments_slice),
344-
Box::from_raw(default_arguments_slice),
345-
)
333+
for info in arguments.iter() {
334+
// SAFETY: These infos were originally created from a call to `PropertyInfo::into_owned_property_sys`, and this method
335+
// will not be called again on this pointer.
336+
unsafe { PropertyInfo::free_owned_property_sys(*info) }
337+
}
338+
339+
// SAFETY:
340+
// - `from_raw_parts_mut`: `default_arguments` comes from `as_mut_ptr()` on a mutable slice of length `default_argument_count`, and no
341+
// other accesses to the pointer happens for the lifetime of the slice.
342+
// - `Box::from_raw`: The slice was returned from a call to `Box::leak`, and we have ownership of the value behind this pointer.
343+
let default_arguments = unsafe {
344+
let slice = std::slice::from_raw_parts_mut(
345+
default_arguments,
346+
u32_to_usize(default_argument_count),
347+
);
348+
349+
Box::from_raw(slice)
346350
};
347351

348-
default_arguments.iter().for_each(|ptr| {
349-
// SAFETY: These pointers were originally created from a call to `Variant::into_owner_var_sys`, and this method will not be
352+
for variant in default_arguments.iter() {
353+
// SAFETY: These pointers were originally created from a call to `Variant::into_owned_var_sys`, and this method will not be
350354
// called again on this pointer.
351-
let _variant = unsafe { Variant::from_owned_var_sys(*ptr) };
352-
});
355+
let _variant = unsafe { Variant::from_owned_var_sys(*variant) };
356+
}
353357
}
354358
}

0 commit comments

Comments
 (0)