Skip to content

Commit d8577c9

Browse files
committed
Document changes, cover subarray_... methods as well.
1 parent 2c48248 commit d8577c9

File tree

6 files changed

+27
-23
lines changed

6 files changed

+27
-23
lines changed

godot-codegen/src/generator/functions_common.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,7 @@ pub(crate) enum FnKind {
360360
/// `call()` forwarding to `try_call()`.
361361
DelegateTry,
362362

363-
// Currently not used – uncomment when needed again.
364-
// /// Default extender `new()` associated function -- optional receiver and required parameters.
365-
// ExBuilderConstructor,
366-
/// Same as [`ExBuilderConstructor`], but for a builder with an explicit lifetime.
363+
/// Default extender `new()` associated function -- optional receiver and required parameters. Has an explicit lifetime.
367364
ExBuilderConstructorLifetimed,
368365

369366
/// Default extender `new()` associated function -- only default parameters.
@@ -576,7 +573,6 @@ pub(crate) fn make_params_exprs<'a>(
576573
// Methods relevant in the context of default parameters. Flow in this order.
577574
// Note that for builder methods of Ex* structs, there's a direct call in default_parameters.rs to the parameter manipulation methods,
578575
// bypassing this method. So one case is missing here.
579-
// FnKind::ExBuilderConstructor => (FnParamDecl::FnPublic, FnArgExpr::StoreInField),
580576
FnKind::ExBuilderConstructorLifetimed => {
581577
(FnParamDecl::FnPublicLifetime, FnArgExpr::StoreInField)
582578
}

godot-codegen/src/models/domain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ pub enum RustTy {
718718

719719
/// `Array<T>`
720720
///
721-
///
721+
/// Set by [`builtin_method_generic_ret`](crate::special_cases::builtin_method_generic_ret)
722722
GenericArray,
723723

724724
/// C-style raw pointer to a `RustTy`.

godot-codegen/src/models/domain_mapping.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -366,18 +366,19 @@ impl BuiltinMethod {
366366
return None;
367367
}
368368

369-
let return_value =
370-
if let Some(generic) = special_cases::is_builtin_method_generic(builtin_name, method) {
371-
generic
372-
} else {
373-
FnReturn::new(
374-
&method
375-
.return_type
376-
.as_deref()
377-
.map(JsonMethodReturn::from_type_no_meta),
378-
ctx,
379-
)
380-
};
369+
let return_value = if let Some(generic) =
370+
special_cases::builtin_method_generic_ret(builtin_name, method)
371+
{
372+
generic
373+
} else {
374+
FnReturn::new(
375+
&method
376+
.return_type
377+
.as_deref()
378+
.map(JsonMethodReturn::from_type_no_meta),
379+
ctx,
380+
)
381+
};
381382

382383
Some(Self {
383384
common: FunctionCommon {

godot-codegen/src/special_cases/special_cases.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,18 @@ pub fn is_builtin_method_deleted(_class_name: &TyName, method: &JsonBuiltinMetho
778778
codegen_special_cases::is_builtin_method_excluded(method)
779779
}
780780

781-
pub fn is_builtin_method_generic(
781+
/// Returns some generic type – such as `GenericArray` representing `Array<T>` – if method is marked as generic, `None` otherwise.
782+
///
783+
/// Usually required to properly initialize the return value & cache its type.
784+
pub fn builtin_method_generic_ret(
782785
class_name: &TyName,
783786
method: &JsonBuiltinMethod,
784787
) -> Option<FnReturn> {
785788
match (
786789
class_name.rust_ty.to_string().as_str(),
787790
method.name.as_str(),
788791
) {
789-
("Array", "duplicate") => Some(FnReturn {
792+
("Array", "duplicate") | ("Array", "slice") => Some(FnReturn {
790793
decl: RustTy::GenericArray.return_decl(),
791794
type_: Some(RustTy::GenericArray),
792795
}),

godot-core/src/builtin/collections/array.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ impl<T: ArrayElement> Array<T> {
589589
let end = end.unwrap_or(i32::MAX as i64);
590590

591591
// SAFETY: The type of the array is `T` and we convert the returned array to an `Array<T>` immediately.
592-
let subarray: VariantArray =
593-
unsafe { self.as_inner().slice(begin, end, step as i64, deep) };
592+
let subarray: Self = unsafe { self.as_inner().slice(begin, end, step as i64, deep) };
594593

595594
// SAFETY: slice() returns a typed array with the same type as Self.
596595
let result = unsafe { subarray.assume_type() };

itest/rust/src/builtin_tests/containers/array_test.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,18 @@ func make_array() -> Array[CustomScriptForArrays]:
677677
assert_eq!(script.get_global_name(), "CustomScriptForArrays".into());
678678
}
679679

680+
// Test that proper type has been set&cached while creating new Array.
680681
#[itest]
681682
fn array_inner_type() {
682683
let primary = Array::<Dictionary>::new();
683684
let secondary = primary.duplicate_shallow();
684685
assert_eq!(secondary.element_type(), primary.element_type());
685686
let secondary = primary.duplicate_deep();
686-
assert_eq!(secondary.element_type(), primary.element_type())
687+
assert_eq!(secondary.element_type(), primary.element_type());
688+
let subarray = primary.subarray_deep(.., None);
689+
assert_eq!(subarray.element_type(), primary.element_type());
690+
let subarray = primary.subarray_shallow(.., None);
691+
assert_eq!(subarray.element_type(), primary.element_type());
687692
}
688693

689694
// ----------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)