-
-
Notifications
You must be signed in to change notification settings - Fork 236
Type-safe call_deferred
alternative
#1204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
goatfryed
wants to merge
6
commits into
godot-rust:master
Choose a base branch
from
goatfryed:feat/1199-type-safe-call-deferred
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0c1ea72
test(core): test Object::call_deferred
goatfryed 0fa23b2
feat(godot-ffi): stub is_main_thread() for wasm_nothread
goatfryed e10faf7
chore(core): add todo
goatfryed e85e5e8
feat(core): support easier, type-safe deferred calls
goatfryed 002a2ce
feat(core): support easier, type-safe deferred calls
goatfryed 40792fc
feat(core): lift FnMut restriction of apply_deferred
goatfryed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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 crate::builtin::{Callable, Variant}; | ||
use crate::meta::UniformObjectDeref; | ||
use crate::obj::bounds::Declarer; | ||
use crate::obj::GodotClass; | ||
#[cfg(since_api = "4.2")] | ||
use crate::registry::signal::ToSignalObj; | ||
use godot_ffi::is_main_thread; | ||
use std::ops::DerefMut; | ||
|
||
// Dummy traits to still allow bounds and imports. | ||
#[cfg(before_api = "4.2")] | ||
pub trait WithDeferredCall<T: GodotClass> {} | ||
|
||
/// Trait that is automatically implemented for engine classes and user classes containing a `Base<T>` field. | ||
/// | ||
/// This trait enables type safe deferred method calls. | ||
/// | ||
/// # Usage | ||
/// | ||
/// ```no_run | ||
/// # use godot::prelude::*; | ||
/// # use std::f32::consts::PI; | ||
/// fn some_fn(mut node: Gd<Node2D>) | ||
/// { | ||
/// node.apply_deferred(|shape_mut| shape_mut.rotate(PI)) | ||
/// } | ||
/// ``` | ||
#[cfg(since_api = "4.2")] | ||
pub trait WithDeferredCall<T: GodotClass> { | ||
/// Runs the given Closure deferred. | ||
/// | ||
/// This is a type-safe alternative to [`Object::call_deferred`][crate::classes::Object::call_deferred]. | ||
/// | ||
/// # Panics | ||
/// If called outside the main thread. | ||
fn apply_deferred<F>(&mut self, rust_function: F) | ||
where | ||
F: FnOnce(&mut T) + 'static; | ||
} | ||
|
||
#[cfg(since_api = "4.2")] | ||
impl<T, S, D> WithDeferredCall<T> for S | ||
where | ||
T: UniformObjectDeref<D, Declarer = D>, | ||
S: ToSignalObj<T>, | ||
D: Declarer, | ||
{ | ||
fn apply_deferred<'a, F>(&mut self, rust_function: F) | ||
where | ||
F: FnOnce(&mut T) + 'static, | ||
{ | ||
assert!( | ||
is_main_thread(), | ||
"`apply_deferred` must be called on the main thread" | ||
); | ||
let mut rust_fn_once = Some(rust_function); | ||
let mut this = self.to_signal_obj().clone(); | ||
let callable = Callable::from_local_fn("apply_deferred", move |_| { | ||
let rust_fn_once = rust_fn_once | ||
.take() | ||
.expect("rust_fn_once was already consumed"); | ||
let mut this_mut = T::object_as_mut(&mut this); | ||
rust_fn_once(this_mut.deref_mut()); | ||
Ok(Variant::nil()) | ||
}); | ||
callable.call_deferred(&[]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* 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 crate::framework::itest; | ||
use godot::obj::WithBaseField; | ||
use godot::prelude::*; | ||
use godot::task::{SignalFuture, TaskHandle}; | ||
use std::ops::DerefMut; | ||
|
||
const ACCEPTED_NAME: &str = "touched"; | ||
|
||
#[derive(GodotClass)] | ||
#[class(init,base=Node2D)] | ||
struct DeferredTestNode { | ||
base: Base<Node2D>, | ||
} | ||
|
||
#[godot_api] | ||
impl DeferredTestNode { | ||
#[signal] | ||
fn test_completed(name: StringName); | ||
|
||
#[func] | ||
fn accept(&mut self) { | ||
self.base_mut().set_name(ACCEPTED_NAME); | ||
} | ||
|
||
fn to_assertion_task(&self) -> TaskHandle { | ||
assert_ne!( | ||
self.base().get_name().to_string(), | ||
ACCEPTED_NAME, | ||
"accept evaluated synchronously" | ||
); | ||
|
||
let test_will_succeed: SignalFuture<(StringName,)> = | ||
Signal::from_object_signal(&self.to_gd(), "test_completed").to_future(); | ||
|
||
godot::task::spawn(async move { | ||
let (name,) = test_will_succeed.await; | ||
|
||
assert_eq!(name.to_string(), ACCEPTED_NAME); | ||
}) | ||
} | ||
} | ||
|
||
#[godot_api] | ||
impl INode2D for DeferredTestNode { | ||
fn process(&mut self, _delta: f64) { | ||
let name = self.base().get_name(); | ||
self.signals().test_completed().emit(&name); | ||
self.base_mut().queue_free(); | ||
} | ||
|
||
fn ready(&mut self) { | ||
self.base_mut().set_name("verify") | ||
} | ||
} | ||
|
||
#[itest(async)] | ||
fn call_deferred_untyped(ctx: &crate::framework::TestContext) -> TaskHandle { | ||
let mut test_node = DeferredTestNode::new_alloc(); | ||
ctx.scene_tree.clone().add_child(&test_node); | ||
|
||
// this is called through godot binding and therefore requires #[func] on the method | ||
test_node.call_deferred("accept", &[]); | ||
|
||
let handle = test_node.bind().to_assertion_task(); | ||
handle | ||
} | ||
|
||
#[itest(async)] | ||
fn call_deferred_godot_class(ctx: &crate::framework::TestContext) -> TaskHandle { | ||
let mut test_node = DeferredTestNode::new_alloc(); | ||
ctx.scene_tree.clone().add_child(&test_node); | ||
|
||
let mut gd_mut = test_node.bind_mut(); | ||
// Explicitly check that this can be invoked on &mut T. | ||
let godot_class_ref: &mut DeferredTestNode = gd_mut.deref_mut(); | ||
godot_class_ref.apply_deferred(DeferredTestNode::accept); | ||
drop(gd_mut); | ||
|
||
let handle = test_node.bind().to_assertion_task(); | ||
handle | ||
} | ||
|
||
#[itest(async)] | ||
fn call_deferred_gd_user_class(ctx: &crate::framework::TestContext) -> TaskHandle { | ||
let mut test_node = DeferredTestNode::new_alloc(); | ||
ctx.scene_tree.clone().add_child(&test_node); | ||
|
||
test_node.apply_deferred(DeferredTestNode::accept); | ||
|
||
let handle = test_node.bind().to_assertion_task(); | ||
handle | ||
} | ||
|
||
#[itest(async)] | ||
fn call_deferred_gd_engine_class(ctx: &crate::framework::TestContext) -> TaskHandle { | ||
let test_node = DeferredTestNode::new_alloc(); | ||
ctx.scene_tree.clone().add_child(&test_node); | ||
|
||
let mut node = test_node.clone().upcast::<Node>(); | ||
node.apply_deferred(|that_node| that_node.set_name(ACCEPTED_NAME)); | ||
|
||
let handle = test_node.bind().to_assertion_task(); | ||
handle | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.