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
//! Thread-local change tracking for custom components with PyObject storage.
//!
//! This module implements lazy change detection that matches Bevy's semantics:
//! - Query[Mut[T]] iteration does NOT mark components as changed
//! - Only actual field mutations (via __setattr__) mark components as changed
//!
//! Architecture:
//! 1. When query iteration starts for an entity, set_entity_context() stores entity + world_ptr
//! 2. When __setattr__ is called, it calls mark_component_changed() with the component_id
//! 3. mark_component_changed() immediately marks that specific component as changed
//!
//! Safety: World pointer is only dereferenced during valid query iteration (protected by ValidityFlag)
use Cell;
use ;
thread_local!
/// Set the entity context at the start of processing an entity in a query.
///
/// This must be called when starting to process each entity in a query iteration.
/// It stores the entity and world pointer for use by mark_component_changed().
///
/// # Safety
/// - world_ptr must be valid for the duration of processing this entity
/// - Must call clear_entity_context() when done with this entity
/// Mark a specific component on the current entity as changed (thread-local context).
///
/// This is called by LazyWrapperProxy's __setattr__ when a field is mutated.
/// It immediately marks the component in Bevy's ECS, no delayed flush needed.
/// Mark a specific component as changed using explicit entity and world pointer.
///
/// Unlike `mark_component_changed()`, this does not rely on the thread-local context.
/// This allows change detection to work for components accessed outside the iteration
/// loop (e.g., items collected via `list(query)`).
///
/// # Safety
/// - `world_ptr` must be valid (protected by ValidityFlag on the caller)
/// - `entity` must exist in the world (guaranteed during system execution)
/// Clear the entity context.
///
/// This must be called when done processing an entity.