Skip to content

Commit 99a0d34

Browse files
bors[bot]Bromeon
andauthored
Merge #823
823: Instance and TInstance: Own=Shared by default r=Bromeon a=Bromeon Simplifies most of `Instance` usages, and is consistent with `Ref`/`TRef`. bors try Co-authored-by: Jan Haller <[email protected]>
2 parents c3dab06 + c45d2b4 commit 99a0d34

File tree

11 files changed

+25
-29
lines changed

11 files changed

+25
-29
lines changed

examples/resource/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ struct Greeter {
2626
// All these traits are implemented for `Instance<T, Shared>` where the base class of `T` is reference-counted.
2727
// `Resource` inherits from `Reference`, so all native scripts extending `Resource` have reference-counted base classes.
2828
#[property]
29-
greeting_resource: Option<Instance<GreetingResource, Shared>>,
29+
greeting_resource: Option<Instance<GreetingResource>>,
3030
}
3131

3232
#[methods]

gdnative-async/src/method.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use futures_task::{LocalFutureObj, LocalSpawn, SpawnError};
77
use gdnative_core::core_types::{ToVariant, Variant};
88
use gdnative_core::export::{Method, NativeClass, Varargs};
99
use gdnative_core::log::{self, Site};
10-
use gdnative_core::object::ownership::Shared;
1110
use gdnative_core::object::TInstance;
1211

1312
use crate::rt::Context;
@@ -41,7 +40,7 @@ pub trait AsyncMethod<C: NativeClass>: Send + Sync + 'static {
4140
pub struct Spawner<'a, C: NativeClass> {
4241
sp: &'static dyn LocalSpawn,
4342
ctx: Context,
44-
this: TInstance<'a, C, Shared>,
43+
this: TInstance<'a, C>,
4544
args: Varargs<'a>,
4645
result: &'a mut Option<Result<(), SpawnError>>,
4746
/// Remove Send and Sync
@@ -54,7 +53,7 @@ impl<'a, C: NativeClass> Spawner<'a, C> {
5453
/// future types.
5554
pub fn spawn<F, R>(self, f: F)
5655
where
57-
F: FnOnce(Arc<Context>, TInstance<'_, C, Shared>, Varargs<'_>) -> R,
56+
F: FnOnce(Arc<Context>, TInstance<'_, C>, Varargs<'_>) -> R,
5857
R: Future<Output = Variant> + 'static,
5958
{
6059
let ctx = Arc::new(self.ctx);
@@ -84,7 +83,7 @@ impl<F> Async<F> {
8483
}
8584

8685
impl<C: NativeClass, F: AsyncMethod<C>> Method<C> for Async<F> {
87-
fn call(&self, this: TInstance<'_, C, Shared>, args: Varargs<'_>) -> Variant {
86+
fn call(&self, this: TInstance<'_, C>, args: Varargs<'_>) -> Variant {
8887
if let Some(sp) = crate::executor::local_spawn() {
8988
let ctx = Context::new();
9089
let func_state = ctx.func_state();

gdnative-async/src/rt.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use func_state::FuncState;
44
use gdnative_bindings::Object;
55
use gdnative_core::core_types::{GodotError, Variant};
66
use gdnative_core::init::InitHandle;
7-
use gdnative_core::object::ownership::Shared;
87
use gdnative_core::object::{Instance, SubClass, TInstance, TRef};
98

109
use crate::future;
@@ -14,7 +13,7 @@ mod func_state;
1413

1514
/// Context for creating `yield`-like futures in async methods.
1615
pub struct Context {
17-
func_state: Instance<FuncState, Shared>,
16+
func_state: Instance<FuncState>,
1817
/// Remove Send and Sync
1918
_marker: PhantomData<*const ()>,
2019
}
@@ -27,11 +26,11 @@ impl Context {
2726
}
2827
}
2928

30-
pub(crate) fn func_state(&self) -> Instance<FuncState, Shared> {
29+
pub(crate) fn func_state(&self) -> Instance<FuncState> {
3130
self.func_state.clone()
3231
}
3332

34-
fn safe_func_state(&self) -> TInstance<'_, FuncState, Shared> {
33+
fn safe_func_state(&self) -> TInstance<'_, FuncState> {
3534
// SAFETY: FuncState objects are bound to their origin threads in Rust, and
3635
// Context is !Send, so this is safe to call within this type.
3736
// Non-Rust code is expected to be following the official guidelines as per

gdnative-async/src/rt/bridge.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use gdnative_core::core_types::{GodotError, Variant, VariantArray};
88
use gdnative_core::export::user_data::{ArcData, Map};
99
use gdnative_core::export::{ClassBuilder, Method, NativeClass, NativeClassMethods, Varargs};
1010
use gdnative_core::godot_site;
11-
use gdnative_core::object::ownership::Shared;
1211
use gdnative_core::object::{Instance, TInstance, TRef};
1312

1413
use crate::future::Resume;
@@ -26,7 +25,7 @@ pub(super) fn terminate() {
2625
#[derive(Default)]
2726
struct Pool {
2827
busy: HashMap<i64, Entry>,
29-
free: Vec<(i64, Instance<SignalBridge, Shared>)>,
28+
free: Vec<(i64, Instance<SignalBridge>)>,
3029
next_id: i64,
3130
}
3231

@@ -42,7 +41,7 @@ struct Entry {
4241
resume: Resume<Vec<Variant>>,
4342

4443
// Just need to keep this alive.
45-
_obj: Instance<SignalBridge, Shared>,
44+
_obj: Instance<SignalBridge>,
4645
}
4746

4847
pub(super) struct SignalBridge {
@@ -97,7 +96,7 @@ impl SignalBridge {
9796
struct OnSignalFn;
9897

9998
impl Method<SignalBridge> for OnSignalFn {
100-
fn call(&self, this: TInstance<'_, SignalBridge, Shared>, args: Varargs<'_>) -> Variant {
99+
fn call(&self, this: TInstance<'_, SignalBridge>, args: Varargs<'_>) -> Variant {
101100
let args = args.cloned().collect();
102101

103102
let this_persist = this.clone().claim();

gdnative-async/src/rt/func_state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use gdnative_core::export::{
66
SignalArgument, StaticArgs, StaticArgsMethod,
77
};
88
use gdnative_core::godot_site;
9-
use gdnative_core::object::ownership::{Shared, Unique};
9+
use gdnative_core::object::ownership::Unique;
1010
use gdnative_core::object::{Instance, TInstance};
1111
use gdnative_derive::FromVarargs;
1212

@@ -57,7 +57,7 @@ impl FuncState {
5757
}
5858
}
5959

60-
pub(super) fn resolve(this: TInstance<'_, FuncState, Shared>, value: Variant) {
60+
pub(super) fn resolve(this: TInstance<'_, FuncState>, value: Variant) {
6161
this.script()
6262
.map_mut(|s| {
6363
match s.kind {
@@ -80,7 +80,7 @@ pub(super) fn resolve(this: TInstance<'_, FuncState, Shared>, value: Variant) {
8080
this.base().emit_signal("completed", &[value]);
8181
}
8282

83-
pub(super) fn make_resumable(this: TInstance<'_, FuncState, Shared>, resume: Resume<Variant>) {
83+
pub(super) fn make_resumable(this: TInstance<'_, FuncState>, resume: Resume<Variant>) {
8484
let kind = this
8585
.script()
8686
.map_mut(|s| std::mem::replace(&mut s.kind, Kind::Resumable(resume)))
@@ -113,7 +113,7 @@ struct IsValidArgs {
113113

114114
impl StaticArgsMethod<FuncState> for IsValidFn {
115115
type Args = IsValidArgs;
116-
fn call(&self, this: TInstance<'_, FuncState, Shared>, args: Self::Args) -> Variant {
116+
fn call(&self, this: TInstance<'_, FuncState>, args: Self::Args) -> Variant {
117117
if args.extended_check.is_some() {
118118
gdnative_core::log::warn(
119119
Self::site().unwrap(),
@@ -145,7 +145,7 @@ struct ResumeArgs {
145145

146146
impl StaticArgsMethod<FuncState> for ResumeFn {
147147
type Args = ResumeArgs;
148-
fn call(&self, this: TInstance<'_, FuncState, Shared>, args: Self::Args) -> Variant {
148+
fn call(&self, this: TInstance<'_, FuncState>, args: Self::Args) -> Variant {
149149
this.map_mut(
150150
|s, owner| match std::mem::replace(&mut s.kind, Kind::Pending) {
151151
Kind::Resumable(resume) => {

gdnative-bindings/src/utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Utility functions and extension traits that depend on generated bindings
22
33
use gdnative_core::export::NativeClass;
4-
use gdnative_core::object::ownership::Shared;
54
use gdnative_core::object::{SubClass, TInstance, TRef};
65

76
use super::generated::{Engine, Node, SceneTree};
@@ -58,7 +57,7 @@ pub trait NodeExt {
5857
/// invariants must be observed for the resulting node during `'a`, if any.
5958
///
6059
/// [thread-safety]: https://docs.godotengine.org/en/stable/tutorials/threads/thread_safe_apis.html
61-
unsafe fn get_node_as_instance<'a, T>(&self, path: &str) -> Option<TInstance<'a, T, Shared>>
60+
unsafe fn get_node_as_instance<'a, T>(&self, path: &str) -> Option<TInstance<'a, T>>
6261
where
6362
T: NativeClass,
6463
T::Base: SubClass<Node>,

gdnative-core/src/export/class_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<C: NativeClass> ClassBuilder<C> {
140140
/// // Now, wrap the method (this can do anything and does not need to actually call a method)
141141
/// struct MyMethod;
142142
/// impl Method<MyType> for MyMethod {
143-
/// fn call(&self, this: TInstance<'_, MyType, Shared>, _args: Varargs<'_>) -> Variant {
143+
/// fn call(&self, this: TInstance<'_, MyType>, _args: Varargs<'_>) -> Variant {
144144
/// this.map(|obj: &MyType, _| {
145145
/// let result = obj.my_method();
146146
/// Variant::new(result)

gdnative-core/src/export/method.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub struct ScriptMethod<'l> {
140140
/// Safe low-level trait for stateful, variadic methods that can be called on a native script type.
141141
pub trait Method<C: NativeClass>: Send + Sync + 'static {
142142
/// Calls the method on `this` with `args`.
143-
fn call(&self, this: TInstance<'_, C, Shared>, args: Varargs<'_>) -> Variant;
143+
fn call(&self, this: TInstance<'_, C>, args: Varargs<'_>) -> Variant;
144144

145145
/// Returns an optional site where this method is defined. Used for logging errors in FFI wrappers.
146146
///
@@ -157,7 +157,7 @@ struct Stateless<F> {
157157
}
158158

159159
impl<C: NativeClass, F: Method<C> + Copy + Default> Method<C> for Stateless<F> {
160-
fn call(&self, this: TInstance<'_, C, Shared>, args: Varargs<'_>) -> Variant {
160+
fn call(&self, this: TInstance<'_, C>, args: Varargs<'_>) -> Variant {
161161
let f = F::default();
162162
f.call(this, args)
163163
}
@@ -182,7 +182,7 @@ impl<F> StaticArgs<F> {
182182
/// "static method".
183183
pub trait StaticArgsMethod<C: NativeClass>: Send + Sync + 'static {
184184
type Args: FromVarargs;
185-
fn call(&self, this: TInstance<'_, C, Shared>, args: Self::Args) -> Variant;
185+
fn call(&self, this: TInstance<'_, C>, args: Self::Args) -> Variant;
186186

187187
/// Returns an optional site where this method is defined. Used for logging errors in FFI wrappers.
188188
///
@@ -195,7 +195,7 @@ pub trait StaticArgsMethod<C: NativeClass>: Send + Sync + 'static {
195195

196196
impl<C: NativeClass, F: StaticArgsMethod<C>> Method<C> for StaticArgs<F> {
197197
#[inline]
198-
fn call(&self, this: TInstance<'_, C, Shared>, mut args: Varargs<'_>) -> Variant {
198+
fn call(&self, this: TInstance<'_, C>, mut args: Varargs<'_>) -> Variant {
199199
match args.read_many::<F::Args>() {
200200
Ok(parsed) => {
201201
if let Err(err) = args.done() {

gdnative-core/src/object/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::private::{get_api, ReferenceCountedClassPlaceholder};
2020
///
2121
/// See the type-level documentation on `Ref` for more information on typed thread accesses.
2222
#[derive(Debug)]
23-
pub struct Instance<T: NativeClass, Own: Ownership> {
23+
pub struct Instance<T: NativeClass, Own: Ownership = Shared> {
2424
owner: Ref<T::Base, Own>,
2525
script: T::UserData,
2626
}
@@ -30,7 +30,7 @@ pub struct Instance<T: NativeClass, Own: Ownership> {
3030
///
3131
/// See the type-level documentation on `Ref` for more information on typed thread accesses.
3232
#[derive(Debug)]
33-
pub struct TInstance<'a, T: NativeClass, Own: Ownership> {
33+
pub struct TInstance<'a, T: NativeClass, Own: Ownership = Shared> {
3434
owner: TRef<'a, T::Base, Own>,
3535
script: T::UserData,
3636
}

test/src/test_map_owned.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct VecBuilder {
2424
#[methods]
2525
impl VecBuilder {
2626
#[export]
27-
fn append(mut self, _owner: TRef<Reference>, mut numbers: Vec<i32>) -> Instance<Self, Shared> {
27+
fn append(mut self, _owner: TRef<Reference>, mut numbers: Vec<i32>) -> Instance<Self> {
2828
self.v.append(&mut numbers);
2929
Instance::emplace(Self { v: self.v }).into_shared()
3030
}

0 commit comments

Comments
 (0)