Skip to content

Commit

Permalink
Expose bump allocator in Sender (#65)
Browse files Browse the repository at this point in the history
This PR removes the `'static` requirement from `Sender::send` and
`Sender::send_to` and exposes the bump allocator by providing allocation
methods on `Sender`. `Sender` is now entirely internally mutable (takes
`&self` on all the methods).

This partially addresses #44 but is missing a few things.
- Doesn't allow received events with borrowed data to pass though
`Sender::send` without a clone because `Receiver` and `Sender` have
different lifetimes. Might still need a combined `Sender` and `Receiver`
type.
- Can't use the bump allocator from a `&mut World`.
- No optional `collections` module. I think it would be easier to just
add an unstable feature for the unstable allocator api for those who
need it.
  • Loading branch information
rj00a authored May 19, 2024
1 parent 091a23a commit d21420b
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 162 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Unreleased

- `Sender` now has allocation methods and can send events with data borrowed from the allocator.
- `Sender` is now entirely internally mutable and all methods take `&self`.
- Changed API of `UnsafeWorldCell`.

## 0.6.0 - 2024-05-18

- Removed `Send + Sync` requirements from all data in the `World`, including components, handlers, and events. `World` is now `!Send + !Sync`.
Expand Down
12 changes: 6 additions & 6 deletions benches/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ fn send_many_global_events(bencher: Bencher) {
world.add_handler(get_b_send_c);
world.add_handler(get_c_send_d);

fn get_a_send_b(r: Receiver<A>, mut s: Sender<B>) {
fn get_a_send_b(r: Receiver<A>, s: Sender<B>) {
s.send(B(r.event.0));
s.send(B(r.event.0));
s.send(B(r.event.0));
s.send(B(r.event.0));
s.send(B(r.event.0));
}

fn get_b_send_c(r: Receiver<B>, mut s: Sender<C>) {
fn get_b_send_c(r: Receiver<B>, s: Sender<C>) {
s.send(C(r.event.0));
s.send(C(r.event.0));
s.send(C(r.event.0));
s.send(C(r.event.0));
s.send(C(r.event.0));
}

fn get_c_send_d(r: Receiver<C>, mut s: Sender<D>) {
fn get_c_send_d(r: Receiver<C>, s: Sender<D>) {
s.send(D(r.event.0));
s.send(D(r.event.0));
s.send(D(r.event.0));
Expand Down Expand Up @@ -83,23 +83,23 @@ fn send_many_targeted_events(bencher: Bencher) {
world.add_handler(get_b_send_c);
world.add_handler(get_c_send_d);

fn get_a_send_b(r: Receiver<A, (EntityId, &A)>, mut s: Sender<B>) {
fn get_a_send_b(r: Receiver<A, (EntityId, &A)>, s: Sender<B>) {
s.send_to(r.query.0, B(r.query.1 .0));
s.send_to(r.query.0, B(r.query.1 .0));
s.send_to(r.query.0, B(r.query.1 .0));
s.send_to(r.query.0, B(r.query.1 .0));
s.send_to(r.query.0, B(r.query.1 .0));
}

fn get_b_send_c(r: Receiver<B, (EntityId, &B)>, mut s: Sender<C>) {
fn get_b_send_c(r: Receiver<B, (EntityId, &B)>, s: Sender<C>) {
s.send_to(r.query.0, C(r.query.1 .0));
s.send_to(r.query.0, C(r.query.1 .0));
s.send_to(r.query.0, C(r.query.1 .0));
s.send_to(r.query.0, C(r.query.1 .0));
s.send_to(r.query.0, C(r.query.1 .0));
}

fn get_c_send_d(r: Receiver<C, (EntityId, &C)>, mut s: Sender<D>) {
fn get_c_send_d(r: Receiver<C, (EntityId, &C)>, s: Sender<D>) {
s.send_to(r.query.0, D(r.query.1 .0));
s.send_to(r.query.0, D(r.query.1 .0));
s.send_to(r.query.0, D(r.query.1 .0));
Expand Down
4 changes: 2 additions & 2 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ mod tests {
b: EntityId,
}

world.add_handler(|_: Receiver<E1>, mut s: Sender<(Despawn, E2)>| {
world.add_handler(|_: Receiver<E1>, s: Sender<(Despawn, E2)>| {
let a = s.spawn();
let b = s.spawn();
s.despawn(b);
s.send(E2 { a, b });
});

world.add_handler(|r: Receiver<E2>, mut s: Sender<()>| {
world.add_handler(|r: Receiver<E2>, s: Sender<()>| {
let c = s.spawn();
assert_ne!(r.event.a, c);
assert_ne!(r.event.b, c);
Expand Down
Loading

0 comments on commit d21420b

Please sign in to comment.