@@ -295,12 +295,29 @@ impl<T: GodotClass> Gd<T> {
295
295
/// println!("Node name: {}", node.upcast_ref().get_name());
296
296
/// }
297
297
/// ```
298
+ ///
299
+ /// Note that this cannot be used to get a reference to Rust classes, for that you should use [`Gd::bind()`]. For instance this
300
+ /// will fail:
301
+ /// ```compile_fail
302
+ /// # use godot::prelude::*;
303
+ /// #[derive(GodotClass)]
304
+ /// #[class(init, base = Node)]
305
+ /// struct SomeClass {}
306
+ ///
307
+ /// #[godot_api]
308
+ /// impl INode for SomeClass {
309
+ /// fn ready(&mut self) {
310
+ /// let other = SomeClass::new_alloc();
311
+ /// let _ = other.upcast_ref::<SomeClass>();
312
+ /// }
313
+ /// }
314
+ /// ```
298
315
pub fn upcast_ref < Base > ( & self ) -> & Base
299
316
where
300
- Base : GodotClass ,
317
+ Base : GodotClass + Bounds < Declarer = bounds :: DeclEngine > ,
301
318
T : Inherits < Base > ,
302
319
{
303
- // SAFETY: valid upcast enforced by Inherits bound .
320
+ // SAFETY: `Base` is guaranteed to be an engine base class of `T` because of the generic bounds .
304
321
unsafe { self . raw . as_upcast_ref :: < Base > ( ) }
305
322
}
306
323
@@ -317,12 +334,29 @@ impl<T: GodotClass> Gd<T> {
317
334
/// node.upcast_mut().set_name(name.into());
318
335
/// }
319
336
/// ```
337
+ ///
338
+ /// Note that this cannot be used to get a mutable reference to Rust classes, for that you should use [`Gd::bind_mut()`]. For instance this
339
+ /// will fail:
340
+ /// ```compile_fail
341
+ /// # use godot::prelude::*;
342
+ /// #[derive(GodotClass)]
343
+ /// #[class(init, base = Node)]
344
+ /// struct SomeClass {}
345
+ ///
346
+ /// #[godot_api]
347
+ /// impl INode for SomeClass {
348
+ /// fn ready(&mut self) {
349
+ /// let mut other = SomeClass::new_alloc();
350
+ /// let _ = other.upcast_mut::<SomeClass>();
351
+ /// }
352
+ /// }
353
+ /// ```
320
354
pub fn upcast_mut < Base > ( & mut self ) -> & mut Base
321
355
where
322
- Base : GodotClass ,
356
+ Base : GodotClass + Bounds < Declarer = bounds :: DeclEngine > ,
323
357
T : Inherits < Base > ,
324
358
{
325
- // SAFETY: valid upcast enforced by Inherits bound .
359
+ // SAFETY: `Base` is guaranteed to be an engine base class of `T` because of the generic bounds .
326
360
unsafe { self . raw . as_upcast_mut :: < Base > ( ) }
327
361
}
328
362
0 commit comments