@@ -17,6 +17,7 @@ mod traits;
17
17
pub mod error;
18
18
pub use class_name:: ClassName ;
19
19
pub use godot_convert:: { FromGodot , GodotConvert , ToGodot } ;
20
+ use sys:: conv:: u32_to_usize;
20
21
pub use traits:: { ArrayElement , GodotType } ;
21
22
22
23
pub ( crate ) use crate :: impl_godot_as_self;
@@ -312,43 +313,46 @@ impl MethodInfo {
312
313
default_arguments,
313
314
} = info;
314
315
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) ) ;
320
329
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)
338
331
} ;
339
332
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)
346
350
} ;
347
351
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
350
354
// 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
+ }
353
357
}
354
358
}
0 commit comments