Skip to content

Commit aea92a5

Browse files
committed
I don't understand Rust
1 parent 886ff0a commit aea92a5

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

crates/bevy_ecs/src/storage/resource_non_send.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,20 +505,20 @@ impl ThreadLocal<'_, '_> {
505505
unreachable!()
506506
};
507507

508+
let system_tick = *self.last_run;
508509
let (result_tx, result_rx) = std::sync::mpsc::sync_channel(1);
509-
let task = |tls: &mut ThreadLocals| {
510+
let task = move |tls: &mut ThreadLocals| {
510511
tls.update_change_tick();
511-
let saved = std::mem::replace(&mut tls.last_tick, *self.last_run);
512+
let saved = std::mem::replace(&mut tls.last_tick, system_tick);
512513
// we want to propagate to caller instead of panicking in the main thread
513-
let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(tls)));
514+
let result =
515+
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| (f(tls), tls.last_tick)));
514516
tls.last_tick = saved;
515517

516-
*self.last_run = tls.last_tick;
517518
result_tx.send(result).unwrap();
518519
};
519520

520521
let task: Box<dyn FnOnce(&mut ThreadLocals) + Send> = Box::new(task);
521-
522522
let task: Box<dyn FnOnce(&mut ThreadLocals) + Send + 'static> =
523523
// SAFETY: This function will block the calling thread until `f` completes,
524524
// so any captured references in `f` will remain valid until then.
@@ -531,7 +531,10 @@ impl ThreadLocal<'_, '_> {
531531

532532
// Wait to receive result back from the main thread.
533533
match result_rx.recv().unwrap() {
534-
Ok(result) => result,
534+
Ok((result, last_run)) => {
535+
*self.last_run = last_run;
536+
result
537+
}
535538
Err(payload) => {
536539
std::panic::resume_unwind(payload);
537540
}

0 commit comments

Comments
 (0)