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
/*
* 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 ;
use crate;
use crate;
/// Unifies dereferencing of user and engine classes, as `&T`/`&mut T` and `Gd<T>`.
///
/// This is mainly used by the `connect_*` functions of [`TypedSignal`](crate::obj::signal::TypedSignal).
///
/// # Motivation
/// Although both user and engine classes are often wrapped in a `Gd<T>`, dereferencing them is done differently depending
/// on whether they are made by the user or engine:
/// - `Gd<EngineClass>` can be deref-ed directly into `&EngineClass` and `&mut EngineClass`.
/// - `Gd<UserClass>` must first go through [`bind()`](Gd::bind)/[`bind_mut()`](Gd::bind_mut), which can finally
/// be deref-ed into `&UserClass` and `&mut UserClass`, respectively.
///
/// Without this trait, there's no clear/generic way of writing functions that can accept both user and engine classes,
/// but need to deref said classes in some way.
///
/// [`UniformObjectDeref`](Self) solves this by explicitly handling each category in a different way, but still resulting
/// in a variable that can be deref-ed into `&T`/`&mut T`.
///
/// # Generic param `Declarer`
/// Rustc [does not acknowledge associated type bounds when checking for overlapping impls](https://github.com/rust-lang/rust/issues/20400),
/// this parameter is essentially used to create 2 different traits, one for each "category" (user or engine).
///
/// Despite being 2 different traits, a function can accept both by simply being generic over `Declarer`:
/// ```no_run
/// use godot::meta::conv::UniformObjectDeref;
/// # use godot::prelude::*;
///
/// fn abstract_over_objects<Declarer, C>(obj: &Gd<C>)
/// where
/// C: UniformObjectDeref<Declarer>,
/// {
/// let ref_provider = UniformObjectDeref::object_as_ref(obj);
/// let obj_ref: &C = & *ref_provider;
/// // Regardless of `Declarer`, we can still deref, since the bounds on
/// // `TargetRef`/`TargetMut` enforce that.
/// }
///
/// #[derive(GodotClass)]
/// #[class(init)]
/// struct MyClass {
/// _base: Base<RefCounted>
/// }
///
/// fn main() {
/// let engine_obj: Gd<RefCounted> = RefCounted::new_gd();
/// let user_obj: Gd<MyClass> = MyClass::new_gd();
///
/// abstract_over_objects(&engine_obj);
/// abstract_over_objects(&user_obj);
/// }
/// ```
///
/// # Similar traits
/// - [`ObjectToOwned`][crate::meta::ObjectToOwned] provides conversion from `&self` or `&Gd<T>` to owned `Gd<T>`.
//
// The crate `https://crates.io/crates/disjoint_impls` handles this in a more user-friendly way, we should
// consider using it if disjoint impls are going to be frequently used.
//
// See also Declarer::DerefTarget in the library, which has a similar but different purpose: finding the nearest engine class
// (`&Node` for `Node`, `&Node` for `MyClass`).
// False positive.
// False positive.
// False positive.