Skip to content

Commit 154b87f

Browse files
bors[bot]ttencate
andauthored
Merge #91
91: Implement packed arrays non-generically r=Bromeon a=ttencate The ideal is to have a `struct PackedArray<T>`, where `T` implements some `PackedArrayElement` trait. But since the autogenerated `InnerPacked*Array` types are entirely independent at the moment and don't implement a common trait, that would require indirecting every method implementation through `PackedArrayElement`, which results in mentioning each method call three times: once in `impl PackedArray<T>`, once in `PackedArrayElement` and once in `impl PackedArrayElement for T`. So, for now, we use a big (but quite straightforward) macro to define each `Packed*Array` as a separate type. Co-authored-by: Thomas ten Cate <[email protected]>
2 parents b989179 + 82a8a16 commit 154b87f

File tree

7 files changed

+764
-47
lines changed

7 files changed

+764
-47
lines changed

godot-core/src/builtin/arrays.rs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use godot_ffi as sys;
88

9-
use crate::builtin::{inner, FromVariant, ToVariant, Variant, VariantConversionError};
9+
use crate::builtin::*;
1010
use crate::obj::Share;
1111
use std::fmt;
1212
use std::marker::PhantomData;
@@ -39,16 +39,6 @@ pub struct Array {
3939
opaque: sys::types::OpaqueArray,
4040
}
4141

42-
impl_builtin_stub!(PackedByteArray, OpaquePackedByteArray);
43-
impl_builtin_stub!(PackedColorArray, OpaquePackedColorArray);
44-
impl_builtin_stub!(PackedFloat32Array, OpaquePackedFloat32Array);
45-
impl_builtin_stub!(PackedFloat64Array, OpaquePackedFloat64Array);
46-
impl_builtin_stub!(PackedInt32Array, OpaquePackedInt32Array);
47-
impl_builtin_stub!(PackedInt64Array, OpaquePackedInt64Array);
48-
impl_builtin_stub!(PackedStringArray, OpaquePackedStringArray);
49-
impl_builtin_stub!(PackedVector2Array, OpaquePackedVector2Array);
50-
impl_builtin_stub!(PackedVector3Array, OpaquePackedVector3Array);
51-
5242
impl_builtin_froms!(Array;
5343
PackedByteArray => array_from_packed_byte_array,
5444
PackedColorArray => array_from_packed_color_array,
@@ -61,16 +51,6 @@ impl_builtin_froms!(Array;
6151
PackedVector3Array => array_from_packed_vector3_array,
6252
);
6353

64-
impl_builtin_froms!(PackedByteArray; Array => packed_byte_array_from_array);
65-
impl_builtin_froms!(PackedColorArray; Array => packed_color_array_from_array);
66-
impl_builtin_froms!(PackedFloat32Array; Array => packed_float32_array_from_array);
67-
impl_builtin_froms!(PackedFloat64Array; Array => packed_float64_array_from_array);
68-
impl_builtin_froms!(PackedInt32Array; Array => packed_int32_array_from_array);
69-
impl_builtin_froms!(PackedInt64Array; Array => packed_int64_array_from_array);
70-
impl_builtin_froms!(PackedStringArray; Array => packed_string_array_from_array);
71-
impl_builtin_froms!(PackedVector2Array; Array => packed_vector2_array_from_array);
72-
impl_builtin_froms!(PackedVector3Array; Array => packed_vector3_array_from_array);
73-
7454
impl Array {
7555
fn from_opaque(opaque: sys::types::OpaqueArray) -> Self {
7656
Self { opaque }
@@ -606,18 +586,6 @@ impl GodotFfi for Array {
606586
}
607587
}
608588

609-
fn to_i64(i: usize) -> i64 {
610-
i.try_into().unwrap()
611-
}
612-
613-
fn to_usize(i: i64) -> usize {
614-
i.try_into().unwrap()
615-
}
616-
617-
fn to_isize(i: usize) -> isize {
618-
i.try_into().unwrap()
619-
}
620-
621589
#[repr(C)]
622590
pub struct TypedArray<T> {
623591
opaque: OpaqueArray,

godot-core/src/builtin/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ mod arrays;
3939
mod color;
4040
mod node_path;
4141
mod others;
42+
mod packed_array;
4243
mod string;
4344
mod string_name;
4445
mod variant;
@@ -55,6 +56,7 @@ pub use arrays::*;
5556
pub use color::*;
5657
pub use node_path::*;
5758
pub use others::*;
59+
pub use packed_array::*;
5860
pub use string::*;
5961
pub use string_name::*;
6062
pub use variant::*;
@@ -71,16 +73,14 @@ pub mod inner {
7173
pub use crate::gen::builtin_classes::*;
7274
}
7375

74-
// pub struct PackedArray<T> {
75-
// _phantom: std::marker::PhantomData<T>
76-
// }
77-
//
78-
// pub type PackedByteArray = PackedArray<u8>;
79-
// pub type PackedInt32Array = PackedArray<i32>;
80-
// pub type PackedInt64Array = PackedArray<i64>;
81-
// pub type PackedFloat32Array = PackedArray<f32>;
82-
// pub type PackedFloat64Array = PackedArray<f64>;
83-
// pub type PackedStringArray = PackedArray<GodotString>;
84-
// pub type PackedVector2Array = PackedArray<Vector2>;
85-
// pub type PackedVector3Array = PackedArray<Vector3>;
86-
// pub type PackedColorArray = PackedArray<Color>;
76+
pub(crate) fn to_i64(i: usize) -> i64 {
77+
i.try_into().unwrap()
78+
}
79+
80+
pub(crate) fn to_usize(i: i64) -> usize {
81+
i.try_into().unwrap()
82+
}
83+
84+
pub(crate) fn to_isize(i: usize) -> isize {
85+
i.try_into().unwrap()
86+
}

0 commit comments

Comments
 (0)