Skip to content

Commit dd48d2d

Browse files
committed
Fix Ex* builders taking ObjectArg<T> instead of impl AsObjectArg<T>
1 parent 15bc053 commit dd48d2d

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

godot-codegen/src/conv/type_conversions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ fn to_rust_type_uncached(full_ty: &GodotTy, ctx: &mut Context) -> RustTy {
236236

237237
RustTy::EngineClass {
238238
tokens: quote! { Gd<#qualified_class> },
239-
arg_view: quote! { ObjectArg<#qualified_class> },
240-
impl_as_arg: quote! { impl AsObjectArg<#qualified_class> },
239+
object_arg: quote! { ObjectArg<#qualified_class> },
240+
impl_as_object_arg: quote! { impl AsObjectArg<#qualified_class> },
241241
inner_class: ty,
242242
}
243243
}
@@ -266,7 +266,7 @@ fn to_rust_expr_inner(expr: &str, ty: &RustTy, is_inner: bool) -> TokenStream {
266266
return match ty {
267267
RustTy::BuiltinIdent(ident) if ident == "Variant" => quote! { Variant::nil() },
268268
RustTy::EngineClass { .. } => {
269-
quote! { ObjectArg::null() }
269+
quote! { Gd::null_arg() }
270270
}
271271
_ => panic!("null not representable in target type {ty:?}"),
272272
}

godot-codegen/src/generator/default_parameters.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,30 +247,33 @@ fn make_extender(
247247
default_value,
248248
} = param;
249249

250-
let type_ = type_.param_decl();
250+
let (field_type, needs_conversion) = type_.private_field_decl();
251251

252252
// Initialize with default parameters where available, forward constructor args otherwise
253253
let init = if let Some(value) = default_value {
254-
quote! { #name: #value }
254+
make_field_init(name, value, needs_conversion)
255255
} else {
256256
quote! { #name }
257257
};
258258

259-
result.builder_fields.push(quote! { #name: #type_ });
259+
result.builder_fields.push(quote! { #name: #field_type });
260260
result.builder_args.push(quote! { self.#name });
261261
result.builder_inits.push(init);
262262
}
263263

264264
for param in default_fn_params {
265265
let FnParam { name, type_, .. } = param;
266-
let type_ = type_.param_decl();
266+
let param_type = type_.param_decl();
267+
let (_, field_needs_conversion) = type_.private_field_decl();
268+
269+
let field_init = make_field_init(name, &quote! { value }, field_needs_conversion);
267270

268271
let method = quote! {
269272
#[inline]
270-
pub fn #name(self, value: #type_) -> Self {
273+
pub fn #name(self, value: #param_type) -> Self {
271274
// Currently not testing whether the parameter was already set
272275
Self {
273-
#name: value,
276+
#field_init,
274277
..self
275278
}
276279
}
@@ -281,3 +284,11 @@ fn make_extender(
281284

282285
result
283286
}
287+
288+
fn make_field_init(name: &Ident, expr: &TokenStream, needs_conversion: bool) -> TokenStream {
289+
if needs_conversion {
290+
quote! { #name: #expr.as_object_arg() }
291+
} else {
292+
quote! { #name: #expr }
293+
}
294+
}

godot-codegen/src/generator/functions_common.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,15 +320,15 @@ pub(crate) fn make_params_exprs<'a>(
320320
// Objects (Gd<T>) use implicit conversions via AsObjectArg. Only use in non-virtual functions.
321321
match &param.type_ {
322322
RustTy::EngineClass {
323-
arg_view,
324-
impl_as_arg,
323+
object_arg,
324+
impl_as_object_arg,
325325
..
326326
} if !is_virtual => {
327327
// Parameter declarations in signature: impl AsObjectArg<T>
328328
if param_is_impl_asarg {
329-
params.push(quote! { #param_name: #impl_as_arg });
329+
params.push(quote! { #param_name: #impl_as_object_arg });
330330
} else {
331-
params.push(quote! { #param_name: #arg_view });
331+
params.push(quote! { #param_name: #object_arg });
332332
}
333333

334334
// Argument names in function body: arg.as_object_arg() vs. arg
@@ -338,7 +338,7 @@ pub(crate) fn make_params_exprs<'a>(
338338
arg_names.push(quote! { #param_name });
339339
}
340340

341-
param_types.push(quote! { #arg_view });
341+
param_types.push(quote! { #object_arg });
342342
}
343343

344344
_ => {

godot-codegen/src/models/domain.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -621,10 +621,10 @@ pub enum RustTy {
621621
tokens: TokenStream,
622622

623623
/// Tokens with `ObjectArg<T>` (used in `type CallSig` tuple types).
624-
arg_view: TokenStream,
624+
object_arg: TokenStream,
625625

626626
/// Signature declaration with `impl AsObjectArg<T>`.
627-
impl_as_arg: TokenStream,
627+
impl_as_object_arg: TokenStream,
628628

629629
/// only inner `T`
630630
#[allow(dead_code)] // only read in minimal config
@@ -639,12 +639,20 @@ impl RustTy {
639639
pub fn param_decl(&self) -> TokenStream {
640640
match self {
641641
RustTy::EngineClass {
642-
arg_view: raw_gd, ..
643-
} => raw_gd.clone(),
642+
impl_as_object_arg, ..
643+
} => impl_as_object_arg.clone(),
644644
other => other.to_token_stream(),
645645
}
646646
}
647647

648+
/// Returns `( <field tokens>, <needs .as_object_arg()> )`.
649+
pub fn private_field_decl(&self) -> (TokenStream, bool) {
650+
match self {
651+
RustTy::EngineClass { object_arg, .. } => (object_arg.clone(), true),
652+
other => (other.to_token_stream(), false),
653+
}
654+
}
655+
648656
pub fn return_decl(&self) -> TokenStream {
649657
match self {
650658
Self::EngineClass { tokens, .. } => quote! { -> Option<#tokens> },

0 commit comments

Comments
 (0)