Skip to content

Commit

Permalink
feat: allow defer to return a value (#137)
Browse files Browse the repository at this point in the history
* feat: allow defer to return a value

* update docs world::defer

---------

Co-authored-by: Indradb <[email protected]>
  • Loading branch information
andrewgazelka and Indra-db authored Jul 14, 2024
1 parent 55320b6 commit 8db4192
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions flecs_ecs/src/core/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ impl World {
/// ```
/// # use flecs_ecs::core::World;
/// # let world = World::new();
/// world.defer(|| {
/// let return_something_if_wanted = world.defer(|| {
/// // deferred operations here
/// });
/// ```
Expand All @@ -642,14 +642,15 @@ impl World {
/// * [`World::is_deferred()`]
/// * C++ API: `world::defer`
#[doc(alias = "world::defer")]
pub fn defer(&self, func: impl FnOnce()) {
pub fn defer<T>(&self, func: impl FnOnce() -> T) -> T {
unsafe {
sys::ecs_defer_begin(self.raw_world.as_ptr());
}
func();
let result = func();
unsafe {
sys::ecs_defer_end(self.raw_world.as_ptr());
}
result
}

/// Suspends deferring of operations but do flush the queue.
Expand Down

0 comments on commit 8db4192

Please sign in to comment.