Skip to content

Commit fc83ad6

Browse files
committed
Docs: recommend releasing bind/base guards when no longer needed
1 parent 1e45609 commit fc83ad6

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

godot-core/src/obj/gd.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,15 @@ use crate::{classes, out};
7171
/// * [`Gd::from_instance_id(id)`][Gd::from_instance_id] and [`Gd::try_from_instance_id(id)`][Gd::try_from_instance_id]
7272
/// to obtain a pointer to an object which is already alive in the engine.
7373
///
74-
/// # Binds
74+
/// # Bind guards
7575
///
7676
/// The [`bind()`][Self::bind] and [`bind_mut()`][Self::bind_mut] methods allow you to obtain a shared or exclusive guard to the user instance.
7777
/// These provide interior mutability similar to [`RefCell`][std::cell::RefCell], with the addition that `Gd` simultaneously handles reference
7878
/// counting (for some types `T`).
7979
///
80+
/// Holding a bind guard will prevent other code paths from obtaining their own shared/mutable bind. As such, you should drop the guard
81+
/// as soon as you don't need it anymore, by closing a `{ }` block or calling `std::mem::drop()`.
82+
///
8083
/// When you declare a `#[func]` method on your own class, and it accepts `&self` or `&mut self`, an implicit `bind()` or `bind_mut()` call
8184
/// on the owning `Gd<T>` is performed. This is important to keep in mind, as you can get into situations that violate dynamic borrow rules; for
8285
/// example if you are inside a `&mut self` method, make a call to GDScript and indirectly call another method on the same object (re-entrancy).
@@ -151,6 +154,8 @@ where
151154
/// `GodotClass` instance, independently of how many `Gd` smart pointers point to it. There are runtime
152155
/// checks to ensure that Rust safety rules (e.g. no `&` and `&mut` coexistence) are upheld.
153156
///
157+
/// Drop the guard as soon as you don't need it anymore. See also [Bind guards](#bind-guards).
158+
///
154159
/// # Panics
155160
/// * If another `Gd` smart pointer pointing to the same Rust instance has a live `GdMut` guard bound.
156161
/// * If there is an ongoing function call from GDScript to Rust, which currently holds a `&mut T`
@@ -167,6 +172,8 @@ where
167172
/// `GodotClass` instance, independently of how many `Gd` smart pointers point to it. There are runtime
168173
/// checks to ensure that Rust safety rules (e.g. no `&mut` aliasing) are upheld.
169174
///
175+
/// Drop the guard as soon as you don't need it anymore. See also [Bind guards](#bind-guards).
176+
///
170177
/// # Panics
171178
/// * If another `Gd` smart pointer pointing to the same Rust instance has a live `GdRef` or `GdMut` guard bound.
172179
/// * If there is an ongoing function call from GDScript to Rust, which currently holds a `&T` or `&mut T`

godot-core/src/obj/script.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ impl<'a, T: ScriptInstance> SiMut<'a, T> {
342342

343343
/// Returns a shared reference suitable for calling engine methods on this object.
344344
///
345+
/// Holding a shared guard prevents other code paths from obtaining a _mutable_ reference to `self`, as such it is recommended to drop the
346+
/// guard as soon as you no longer need it.
347+
///
345348
/// ```no_run
346349
/// # use godot::prelude::*;
347350
/// # use godot::classes::{ScriptLanguage, Script};
@@ -388,7 +391,10 @@ impl<'a, T: ScriptInstance> SiMut<'a, T> {
388391

389392
/// Returns a mutable reference suitable for calling engine methods on this object.
390393
///
391-
/// This method will allow you to call back into the same object from Godot.
394+
/// This method will allow you to call back into the same object from Godot (re-entrancy).
395+
///
396+
/// Holding a mutable guard prevents other code paths from obtaining _any_ reference to `self`, as such it is recommended to drop the
397+
/// guard as soon as you no longer need it.
392398
///
393399
/// ```no_run
394400
/// # use godot::prelude::*;

godot-core/src/obj/traits.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ pub trait WithBaseField: GodotClass + Bounds<Declarer = bounds::DeclUser> {
248248

249249
/// Returns a shared reference suitable for calling engine methods on this object.
250250
///
251+
/// Holding a shared guard prevents other code paths from obtaining a _mutable_ reference to `self`, as such it is recommended to drop the
252+
/// guard as soon as you no longer need it.
253+
///
251254
/// # Examples
252255
///
253256
/// ```no_run
@@ -312,6 +315,9 @@ pub trait WithBaseField: GodotClass + Bounds<Declarer = bounds::DeclUser> {
312315
/// This method will allow you to call back into the same object from Godot, unlike what would happen
313316
/// if you used [`to_gd()`](WithBaseField::to_gd).
314317
///
318+
/// Holding a mutable guard prevents other code paths from obtaining _any_ reference to `self`, as such it is recommended to drop the
319+
/// guard as soon as you no longer need it.
320+
///
315321
/// # Examples
316322
///
317323
/// ```no_run

0 commit comments

Comments
 (0)