You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Document when type inference works and when it doesn't.
- Explicitly enforce `Trait: 'static` for `DynGd<T, D>` and `AsDyn<D>` – mostly for clarity, creating non-static `DynGd<T, D>` was already pretty much impossible, and `T` – GodotClass – must be 'static either way.
Copy file name to clipboardExpand all lines: godot-core/src/obj/dyn_gd.rs
+77-10Lines changed: 77 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -155,11 +155,75 @@ use std::{fmt, ops};
155
155
///
156
156
/// `#[export]` for a `DynGd<T, D>` allows you to limit the available choices to implementors of a given trait `D` whose base inherits the specified `T`
157
157
/// (for example, `#[export] Option<DynGd<Resource, dyn MyTrait>>` won't include Rust classes with an Object base, even if they implement `MyTrait`).
158
+
///
159
+
/// # Type inference
160
+
///
161
+
/// If a class implements more than one `AsDyn<D>` relation (usually via `#[godot_dyn]`), type inference will only work when the trait
162
+
/// used for `D` explicitly declares a `: 'static` bound.
163
+
/// Otherwise, if only one `impl AsDyn` is present for a given class, the type can always be inferred.
164
+
///
165
+
/// ```no_run
166
+
/// # use godot::prelude::*;
167
+
/// trait Health: 'static { /* ... */ }
168
+
///
169
+
/// // Exact equivalent to:
170
+
/// trait OtherHealth
171
+
/// where
172
+
/// Self: 'static
173
+
/// { /* ... */ }
174
+
///
175
+
/// trait NoInference { /* ... */ }
176
+
///
177
+
/// #[derive(GodotClass)]
178
+
/// # #[class(init)]
179
+
/// struct Monster { /* ... */ }
180
+
///
181
+
/// #[godot_dyn]
182
+
/// impl Health for Monster { /* ... */ }
183
+
///
184
+
/// #[godot_dyn]
185
+
/// impl NoInference for Monster { /* ... */ }
186
+
///
187
+
/// // Two example functions accepting trait object, to check type inference.
0 commit comments