Skip to content

Commit 1110e37

Browse files
committed
Document AsObjectArg better
1 parent b9b81ed commit 1110e37

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

godot-core/src/obj/object_arg.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,33 @@ use std::ptr;
1515

1616
/// Objects that can be passed as arguments to Godot engine functions.
1717
///
18-
/// This trait is implemented for the following types:
19-
/// - [`Gd<T>`] and `&Gd<T>`, to pass objects. Subclasses of `T` are explicitly supported.
20-
/// - [`Option<Gd<T>>`] and `Option<&Gd<T>>`, to pass optional objects. `None` is mapped to a null argument.
18+
/// This trait is implemented for **shared references** in multiple ways:
19+
/// - [`&Gd<T>`][crate::obj::Gd] to pass objects. Subclasses of `T` are explicitly supported.
20+
/// - [`Option<&Gd<T>>`][Option], to pass optional objects. `None` is mapped to a null argument.
2121
/// - [`Gd::null_arg()`], to pass `null` arguments without using `Option`.
2222
///
2323
/// # Nullability
2424
/// <div class="warning">
2525
/// The GDExtension API does not inform about nullability of its function parameters. It is up to you to verify that the arguments you pass
2626
/// are only null when this is allowed. Doing this wrong should be safe, but can lead to the function call failing.
2727
/// </div>
28+
///
29+
/// # Different argument types
30+
/// Currently, the trait requires pass-by-ref, which helps detect accidental cloning when interfacing with Godot APIs. Plus, it is more
31+
/// consistent with the [`AsArg`][crate::meta::AsArg] trait (for strings, but also `AsArg<Gd<T>>` as used in
32+
/// [`Array::push()`][crate::builtin::Array::push] and similar methods).
33+
///
34+
/// The following table lists the possible argument types and how you can pass them. `Gd` is short for `Gd<T>`.
35+
///
36+
/// | Type | Closest accepted type | How to transform |
37+
/// |-------------------|-----------------------|------------------|
38+
/// | `Gd` | `&Gd` | `&arg` |
39+
/// | `&Gd` | `&Gd` | `arg` |
40+
/// | `&mut Gd` | `&Gd` | `&*arg` |
41+
/// | `Option<Gd>` | `Option<&Gd>` | `arg.as_ref()` |
42+
/// | `Option<&Gd>` | `Option<&Gd>` | `arg` |
43+
/// | `Option<&mut Gd>` | `Option<&Gd>` | `arg.as_deref()` |
44+
/// | (null literal) | | `Gd::null_arg()` |
2845
#[diagnostic::on_unimplemented(
2946
message = "Argument of type `{Self}` cannot be passed to an `impl AsObjectArg<{T}>` parameter",
3047
note = "If you pass by value, consider borrowing instead.",

0 commit comments

Comments
 (0)