Skip to content

Commit a43739c

Browse files
committed
Implement PartialEq for Packed*Array and Eq where appropriate
1 parent cb3632e commit a43739c

File tree

3 files changed

+169
-131
lines changed

3 files changed

+169
-131
lines changed

godot-core/src/builtin/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ macro_rules! impl_builtin_traits_inner {
4949
fn drop(&mut self) {
5050
unsafe {
5151
let destructor = ::godot_ffi::builtin_fn!($gd_method @1);
52-
destructor(self.sys_mut());
52+
destructor(self.sys_mut());
5353
}
5454
}
5555
}
@@ -71,7 +71,7 @@ macro_rules! impl_builtin_traits_inner {
7171
};
7272

7373
( Eq for $Type:ty => $gd_method:ident ) => {
74-
impl_builtin_traits_inner!(PartialEq for $Type => $gd_method);
74+
impl_builtin_traits_inner!(PartialEq for $Type => $gd_method);
7575
impl Eq for $Type {}
7676
};
7777

godot-core/src/builtin/packed_array.rs

+149-128
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,28 @@ use sys::{ffi_methods, interface_fn, GodotFfi, TagString, TagType};
1616
macro_rules! impl_packed_array {
1717
(
1818
// Name of the type to define, e.g. `PackedByteArray`.
19-
$PackedArray:ident,
19+
type_name: $PackedArray:ident,
2020
// Type of elements contained in the array, e.g. `u8`.
21-
$Element:ty,
21+
element_type: $Element:ty,
2222
// Name of wrapped opaque type, e.g. `OpaquePackedByteArray`.
23-
$Opaque:ty,
23+
opaque_type: $Opaque:ty,
2424
// Name of inner type, e.g. `InnerPackedByteArray`.
25-
$Inner:ident,
25+
inner_type: $Inner:ident,
2626
// Name of type that represents elements in function call arguments, e.g. `i64`. See
2727
// `Self::into_arg()`.
28-
$Arg:ty,
28+
argument_type: $Arg:ty,
2929
// Type that is returned from `$operator_index` and `$operator_index_const`.
30-
$IndexRetType:ty,
31-
// Name of default constructor function from FFI, e.g.
32-
// `packed_byte_array_construct_default`.
33-
$construct_default:ident,
34-
// Name of copy constructor function from FFI, e.g.
35-
// `packed_byte_array_construct_copy`.
36-
$construct_copy:ident,
30+
return_type: $IndexRetType:ty,
3731
// Name of constructor function from `Array` from FFI, e.g. `packed_byte_array_from_array`.
38-
$from_array:ident,
39-
// Name of destructor function from FFI, e.g. `packed_byte_array_destroy`.
40-
$destroy:ident,
32+
from_array: $from_array:ident,
4133
// Name of index operator from FFI, e.g. `packed_byte_array_operator_index`.
42-
$operator_index:ident,
34+
operator_index: $operator_index:ident,
4335
// Name of const index operator from FFI, e.g. `packed_byte_array_operator_index_const`.
44-
$operator_index_const:ident,
36+
operator_index_const: $operator_index_const:ident,
37+
// Invocation passed to `impl_builtin_traits!` macro.
38+
trait_impls: {
39+
$($trait_impls:tt)*
40+
},
4541
) => {
4642
// TODO expand type names in doc comments (use e.g. `paste` crate)
4743
/// Implements Godot's `$PackedArray` type, which is an efficient array of `$Element`s.
@@ -324,9 +320,7 @@ macro_rules! impl_packed_array {
324320

325321
impl_builtin_traits! {
326322
for $PackedArray {
327-
Default => $construct_default;
328-
Clone => $construct_copy;
329-
Drop => $destroy;
323+
$($trait_impls)*
330324
}
331325
}
332326

@@ -413,136 +407,163 @@ macro_rules! impl_packed_array {
413407
}
414408

415409
impl_packed_array!(
416-
PackedByteArray,
417-
u8,
418-
OpaquePackedByteArray,
419-
InnerPackedByteArray,
420-
i64,
421-
u8,
422-
packed_byte_array_construct_default,
423-
packed_byte_array_construct_copy,
424-
packed_byte_array_from_array,
425-
packed_byte_array_destroy,
426-
packed_byte_array_operator_index,
427-
packed_byte_array_operator_index_const,
410+
type_name: PackedByteArray,
411+
element_type: u8,
412+
opaque_type: OpaquePackedByteArray,
413+
inner_type: InnerPackedByteArray,
414+
argument_type: i64,
415+
return_type: u8,
416+
from_array: packed_byte_array_from_array,
417+
operator_index: packed_byte_array_operator_index,
418+
operator_index_const: packed_byte_array_operator_index_const,
419+
trait_impls: {
420+
Default => packed_byte_array_construct_default;
421+
Clone => packed_byte_array_construct_copy;
422+
Drop => packed_byte_array_destroy;
423+
Eq => packed_byte_array_operator_equal;
424+
},
428425
);
429426

430427
impl_packed_array!(
431-
PackedInt32Array,
432-
i32,
433-
OpaquePackedInt32Array,
434-
InnerPackedInt32Array,
435-
i64,
436-
i32,
437-
packed_int32_array_construct_default,
438-
packed_int32_array_construct_copy,
439-
packed_int32_array_from_array,
440-
packed_int32_array_destroy,
441-
packed_int32_array_operator_index,
442-
packed_int32_array_operator_index_const,
428+
type_name: PackedInt32Array,
429+
element_type: i32,
430+
opaque_type: OpaquePackedInt32Array,
431+
inner_type: InnerPackedInt32Array,
432+
argument_type: i64,
433+
return_type: i32,
434+
from_array: packed_int32_array_from_array,
435+
operator_index: packed_int32_array_operator_index,
436+
operator_index_const: packed_int32_array_operator_index_const,
437+
trait_impls: {
438+
Default => packed_int32_array_construct_default;
439+
Clone => packed_int32_array_construct_copy;
440+
Drop => packed_int32_array_destroy;
441+
Eq => packed_int32_array_operator_equal;
442+
},
443443
);
444444

445445
impl_packed_array!(
446-
PackedInt64Array,
447-
i64,
448-
OpaquePackedInt64Array,
449-
InnerPackedInt64Array,
450-
i64,
451-
i64,
452-
packed_int64_array_construct_default,
453-
packed_int64_array_construct_copy,
454-
packed_int64_array_from_array,
455-
packed_int64_array_destroy,
456-
packed_int64_array_operator_index,
457-
packed_int64_array_operator_index_const,
446+
type_name: PackedInt64Array,
447+
element_type: i64,
448+
opaque_type: OpaquePackedInt64Array,
449+
inner_type: InnerPackedInt64Array,
450+
argument_type: i64,
451+
return_type: i64,
452+
from_array: packed_int64_array_from_array,
453+
operator_index: packed_int64_array_operator_index,
454+
operator_index_const: packed_int64_array_operator_index_const,
455+
trait_impls: {
456+
Default => packed_int64_array_construct_default;
457+
Clone => packed_int64_array_construct_copy;
458+
Drop => packed_int64_array_destroy;
459+
Eq => packed_int64_array_operator_equal;
460+
},
458461
);
459462

460463
impl_packed_array!(
461-
PackedFloat32Array,
462-
f32,
463-
OpaquePackedFloat32Array,
464-
InnerPackedFloat32Array,
465-
f64,
466-
f32,
467-
packed_float32_array_construct_default,
468-
packed_float32_array_construct_copy,
469-
packed_float32_array_from_array,
470-
packed_float32_array_destroy,
471-
packed_float32_array_operator_index,
472-
packed_float32_array_operator_index_const,
464+
type_name: PackedFloat32Array,
465+
element_type: f32,
466+
opaque_type: OpaquePackedFloat32Array,
467+
inner_type: InnerPackedFloat32Array,
468+
argument_type: f64,
469+
return_type: f32,
470+
from_array: packed_float32_array_from_array,
471+
operator_index: packed_float32_array_operator_index,
472+
operator_index_const: packed_float32_array_operator_index_const,
473+
trait_impls: {
474+
Default => packed_float32_array_construct_default;
475+
Clone => packed_float32_array_construct_copy;
476+
Drop => packed_float32_array_destroy;
477+
PartialEq => packed_float32_array_operator_equal;
478+
},
473479
);
474480

475481
impl_packed_array!(
476-
PackedFloat64Array,
477-
f64,
478-
OpaquePackedFloat64Array,
479-
InnerPackedFloat64Array,
480-
f64,
481-
f64,
482-
packed_float64_array_construct_default,
483-
packed_float64_array_construct_copy,
484-
packed_float64_array_from_array,
485-
packed_float64_array_destroy,
486-
packed_float64_array_operator_index,
487-
packed_float64_array_operator_index_const,
482+
type_name: PackedFloat64Array,
483+
element_type: f64,
484+
opaque_type: OpaquePackedFloat64Array,
485+
inner_type: InnerPackedFloat64Array,
486+
argument_type: f64,
487+
return_type: f64,
488+
from_array: packed_float64_array_from_array,
489+
operator_index: packed_float64_array_operator_index,
490+
operator_index_const: packed_float64_array_operator_index_const,
491+
trait_impls: {
492+
Default => packed_float64_array_construct_default;
493+
Clone => packed_float64_array_construct_copy;
494+
Drop => packed_float64_array_destroy;
495+
PartialEq => packed_float64_array_operator_equal;
496+
},
488497
);
489498

490499
impl_packed_array!(
491-
PackedStringArray,
492-
GodotString,
493-
OpaquePackedStringArray,
494-
InnerPackedStringArray,
495-
GodotString,
496-
TagString,
497-
packed_string_array_construct_default,
498-
packed_string_array_construct_copy,
499-
packed_string_array_from_array,
500-
packed_string_array_destroy,
501-
packed_string_array_operator_index,
502-
packed_string_array_operator_index_const,
500+
type_name: PackedStringArray,
501+
element_type: GodotString,
502+
opaque_type: OpaquePackedStringArray,
503+
inner_type: InnerPackedStringArray,
504+
argument_type: GodotString,
505+
return_type: TagString,
506+
from_array: packed_string_array_from_array,
507+
operator_index: packed_string_array_operator_index,
508+
operator_index_const: packed_string_array_operator_index_const,
509+
trait_impls: {
510+
Default => packed_string_array_construct_default;
511+
Clone => packed_string_array_construct_copy;
512+
Drop => packed_string_array_destroy;
513+
Eq => packed_string_array_operator_equal;
514+
},
503515
);
504516

505517
impl_packed_array!(
506-
PackedVector2Array,
507-
Vector2,
508-
OpaquePackedVector2Array,
509-
InnerPackedVector2Array,
510-
Vector2,
511-
TagType,
512-
packed_vector2_array_construct_default,
513-
packed_vector2_array_construct_copy,
514-
packed_vector2_array_from_array,
515-
packed_vector2_array_destroy,
516-
packed_vector2_array_operator_index,
517-
packed_vector2_array_operator_index_const,
518+
type_name: PackedVector2Array,
519+
element_type: Vector2,
520+
opaque_type: OpaquePackedVector2Array,
521+
inner_type: InnerPackedVector2Array,
522+
argument_type: Vector2,
523+
return_type: TagType,
524+
from_array: packed_vector2_array_from_array,
525+
operator_index: packed_vector2_array_operator_index,
526+
operator_index_const: packed_vector2_array_operator_index_const,
527+
trait_impls: {
528+
Default => packed_vector2_array_construct_default;
529+
Clone => packed_vector2_array_construct_copy;
530+
Drop => packed_vector2_array_destroy;
531+
PartialEq => packed_vector2_array_operator_equal;
532+
},
518533
);
519534

520535
impl_packed_array!(
521-
PackedVector3Array,
522-
Vector3,
523-
OpaquePackedVector3Array,
524-
InnerPackedVector3Array,
525-
Vector3,
526-
TagType,
527-
packed_vector3_array_construct_default,
528-
packed_vector3_array_construct_copy,
529-
packed_vector3_array_from_array,
530-
packed_vector3_array_destroy,
531-
packed_vector3_array_operator_index,
532-
packed_vector3_array_operator_index_const,
536+
type_name: PackedVector3Array,
537+
element_type: Vector3,
538+
opaque_type: OpaquePackedVector3Array,
539+
inner_type: InnerPackedVector3Array,
540+
argument_type: Vector3,
541+
return_type: TagType,
542+
from_array: packed_vector3_array_from_array,
543+
operator_index: packed_vector3_array_operator_index,
544+
operator_index_const: packed_vector3_array_operator_index_const,
545+
trait_impls: {
546+
Default => packed_vector3_array_construct_default;
547+
Clone => packed_vector3_array_construct_copy;
548+
Drop => packed_vector3_array_destroy;
549+
PartialEq => packed_vector3_array_operator_equal;
550+
},
533551
);
534552

535553
impl_packed_array!(
536-
PackedColorArray,
537-
Color,
538-
OpaquePackedColorArray,
539-
InnerPackedColorArray,
540-
Color,
541-
TagType,
542-
packed_color_array_construct_default,
543-
packed_color_array_construct_copy,
544-
packed_color_array_from_array,
545-
packed_color_array_destroy,
546-
packed_color_array_operator_index,
547-
packed_color_array_operator_index_const,
554+
type_name: PackedColorArray,
555+
element_type: Color,
556+
opaque_type: OpaquePackedColorArray,
557+
inner_type: InnerPackedColorArray,
558+
argument_type: Color,
559+
return_type: TagType,
560+
from_array: packed_color_array_from_array,
561+
operator_index: packed_color_array_operator_index,
562+
operator_index_const: packed_color_array_operator_index_const,
563+
trait_impls: {
564+
Default => packed_color_array_construct_default;
565+
Clone => packed_color_array_construct_copy;
566+
Drop => packed_color_array_destroy;
567+
PartialEq => packed_color_array_operator_equal;
568+
},
548569
);

itest/rust/src/packed_array_test.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
use crate::{expect_panic, itest};
8-
use godot::builtin::PackedByteArray;
8+
use godot::builtin::{PackedByteArray, PackedFloat32Array};
99

1010
pub fn run() -> bool {
1111
let mut ok = true;
@@ -15,6 +15,7 @@ pub fn run() -> bool {
1515
ok &= packed_array_from();
1616
ok &= packed_array_to_vec();
1717
// ok &= packed_array_into_iterator();
18+
ok &= packed_array_eq();
1819
ok &= packed_array_clone();
1920
ok &= packed_array_slice();
2021
ok &= packed_array_get();
@@ -73,6 +74,22 @@ fn packed_array_to_vec() {
7374
// assert_eq!(iter.next(), None);
7475
// }
7576

77+
#[itest]
78+
fn packed_array_eq() {
79+
assert_eq!(
80+
PackedByteArray::from(&[1, 2]),
81+
PackedByteArray::from(&[1, 2])
82+
);
83+
assert_ne!(
84+
PackedByteArray::from(&[1, 2]),
85+
PackedByteArray::from(&[1, 1])
86+
);
87+
assert_ne!(
88+
PackedFloat32Array::from(&[f32::NAN]),
89+
PackedFloat32Array::from(&[f32::NAN])
90+
);
91+
}
92+
7693
#[itest]
7794
fn packed_array_clone() {
7895
let mut array = PackedByteArray::from(&[1, 2]);

0 commit comments

Comments
 (0)