1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* Copyright (c) godot-rust; Bromeon and contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use PhantomData;
use ManuallyDrop;
use ;
use crate;
use cratesys;
/// Borrowed (non-owning) reference to a Godot object.
///
/// Unlike `Gd<T>`, this type does not increment/decrement the reference count for `RefCounted` objects, and dropping it leaves the Godot
/// object untouched. The lifetime `'a` ties it to whatever guarantees the object's validity: a borrow of an existing `Gd` ([`Self::from_gd`]),
/// or a guard's borrow of the instance, unified at guard construction ([`Self::from_obj_sys`]).
///
/// This type is for internal use only, to access base objects in guards and traits. It wraps the manual [`Gd::clone_weak()`] +
/// [`Gd::drop_weak()`] pattern in a misuse-proof RAII API.
///
/// # Design: lifetimes vs. drop check
/// Rust's [drop check](https://doc.rust-lang.org/nomicon/dropck.html) requires that lifetimes _used by a type's destructor_ strictly outlive
/// the value, since the destructor might access the borrowed data. A `Drop` impl directly on `BorrowedGd<'a, T>` would therefore extend every
/// `'a` borrow until the end of scope, instead of ending it at the last use. Guards holding a `BorrowedGd` would then conflict with later
/// mutable borrows of the same object -- false positives, since our cleanup never accesses the borrowed data. (Nightly's `#[may_dangle]`
/// addresses this, but isn't available on stable.)
///
/// Instead, cleanup lives on the nested [`BorrowedStorage`], which has no lifetime parameter. Its destructor thus cannot "see" `'a`, so
/// borrows end at the last use, while cleanup still runs automatically. Future fields that need cleanup must go there, not into
/// `BorrowedGd` -- and `BorrowedGd` itself must never gain a `Drop` impl or a field whose destructor uses `'a`, as either would resurrect
/// the borrow conflicts.
pub
/// Owning part of [`BorrowedGd`]: stores the weak `Gd` and cleans it up on drop. See there for why this is split out.
// Note: We intentionally do NOT implement Clone for BorrowedGd, as cloning weak references requires careful lifetime management that
// should be explicit.