|
1028 | 1028 | <a href="#1027" id="1027">1027</a> |
1029 | 1029 | <a href="#1028" id="1028">1028</a> |
1030 | 1030 | <a href="#1029" id="1029">1029</a> |
1031 | | -<a href="#1030" id="1030">1030</a></pre></div><pre class="rust"><code><span class="attr">#![allow(dead_code)] |
| 1031 | +<a href="#1030" id="1030">1030</a> |
| 1032 | +<a href="#1031" id="1031">1031</a> |
| 1033 | +<a href="#1032" id="1032">1032</a> |
| 1034 | +<a href="#1033" id="1033">1033</a> |
| 1035 | +<a href="#1034" id="1034">1034</a> |
| 1036 | +<a href="#1035" id="1035">1035</a> |
| 1037 | +<a href="#1036" id="1036">1036</a> |
| 1038 | +<a href="#1037" id="1037">1037</a> |
| 1039 | +<a href="#1038" id="1038">1038</a></pre></div><pre class="rust"><code><span class="attr">#![allow(dead_code)] |
1032 | 1040 | </span><span class="kw">use </span>std::{fmt::Debug, future::Future, hash::Hash}; |
1033 | 1041 |
|
1034 | 1042 | <span class="kw">use </span>n0_future::{future, FuturesUnordered}; |
|
1342 | 1350 | <span class="kw">impl</span><P: Params> EntityHandle<P> { |
1343 | 1351 | <span class="kw">pub fn </span>send(<span class="kw-2">&</span><span class="self">self</span>) -> <span class="kw-2">&</span>mpsc::Sender<entity_actor::Command<P>> { |
1344 | 1352 | <span class="kw">match </span><span class="self">self </span>{ |
1345 | | - EntityHandle::Live { send: sender } => sender, |
1346 | | - EntityHandle::ShuttingDown { send: sender, .. } => sender, |
| 1353 | + EntityHandle::Live { send } => send, |
| 1354 | + EntityHandle::ShuttingDown { send, .. } => send, |
1347 | 1355 | } |
1348 | 1356 | } |
1349 | 1357 | } |
|
1456 | 1464 | task |
1457 | 1465 | } |
1458 | 1466 |
|
1459 | | - <span class="attr">#[must_use = <span class="string">"this function may return a future that must be spawned by the caller"</span>] |
| 1467 | + <span class="doccomment">/// This function needs to be polled by the owner of the actor state to advance the |
| 1468 | + /// entity manager state machine. If it returns a future, that future must be spawned |
| 1469 | + /// by the caller. |
| 1470 | + </span><span class="attr">#[must_use = <span class="string">"this function may return a future that must be spawned by the caller"</span>] |
1460 | 1471 | </span><span class="kw">pub async fn </span>tick(<span class="kw-2">&mut </span><span class="self">self</span>) -> <span class="prelude-ty">Option</span><<span class="kw">impl </span>Future<Output = ()> + Send + <span class="lifetime">'static</span>> { |
1461 | 1472 | <span class="kw">if let </span><span class="prelude-val">Some</span>(cmd) = <span class="self">self</span>.internal_recv.recv().<span class="kw">await </span>{ |
1462 | 1473 | <span class="kw">match </span>cmd { |
|
1522 | 1533 | } |
1523 | 1534 |
|
1524 | 1535 | <span class="doccomment">/// Get or create an entity actor for the given id. |
| 1536 | + /// |
| 1537 | + /// If this function returns a future, it must be spawned by the caller. |
1525 | 1538 | </span><span class="kw">fn </span>get_or_create( |
1526 | 1539 | <span class="kw-2">&mut </span><span class="self">self</span>, |
1527 | 1540 | id: P::EntityId, |
|
1531 | 1544 | ) { |
1532 | 1545 | <span class="kw">let </span><span class="kw-2">mut </span>task = <span class="prelude-val">None</span>; |
1533 | 1546 | <span class="kw">let </span>handle = <span class="self">self</span>.live.entry(id.clone()).or_insert_with(|| { |
1534 | | - <span class="kw">if let </span><span class="prelude-val">Some</span>((sender, <span class="kw-2">mut </span>actor)) = <span class="self">self</span>.pool.pop() { |
1535 | | - actor.state.id = id.clone(); |
| 1547 | + <span class="kw">if let </span><span class="prelude-val">Some</span>((send, <span class="kw-2">mut </span>actor)) = <span class="self">self</span>.pool.pop() { |
| 1548 | + <span class="comment">// Get an actor from the pool of inactive actors and initialize it. |
| 1549 | + </span>actor.state.id = id.clone(); |
1536 | 1550 | actor.state.global = <span class="self">self</span>.state.clone(); |
1537 | | - actor.state.state.reset(); |
| 1551 | + <span class="comment">// strictly speaking this is not needed, since we reset the state when adding the actor to the pool. |
| 1552 | + </span>actor.state.state.reset(); |
1538 | 1553 | task = <span class="prelude-val">Some</span>(actor.run()); |
1539 | | - EntityHandle::Live { send: sender } |
| 1554 | + EntityHandle::Live { send } |
1540 | 1555 | } <span class="kw">else </span>{ |
1541 | | - <span class="kw">let </span>(sender, recv) = mpsc::channel(<span class="self">self</span>.entity_inbox_size); |
| 1556 | + <span class="comment">// Create a new entity actor and inbox. |
| 1557 | + </span><span class="kw">let </span>(send, recv) = mpsc::channel(<span class="self">self</span>.entity_inbox_size); |
1542 | 1558 | <span class="kw">let </span>state: entity_actor::State<P> = entity_actor::State { |
1543 | 1559 | id: id.clone(), |
1544 | 1560 | global: <span class="self">self</span>.state.clone(), |
|
1553 | 1569 | ), |
1554 | 1570 | }; |
1555 | 1571 | task = <span class="prelude-val">Some</span>(actor.run()); |
1556 | | - EntityHandle::Live { send: sender } |
| 1572 | + EntityHandle::Live { send } |
1557 | 1573 | } |
1558 | 1574 | }); |
1559 | 1575 | (handle, task) |
|
0 commit comments