Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Borrow instead of consuming in EventReader::clear #6851

Closed
wants to merge 3 commits into from

Conversation

mvlabat
Copy link
Contributor

@mvlabat mvlabat commented Dec 4, 2022

The PR fixes the interface of EventReader::clear. Currently, the method consumes the reader, which makes it unusable.

Changelog

  • EventReader::clear now takes a mutable reference instead of consuming the event reader.

Migration Guide

EventReader::clear now takes a mutable reference instead of consuming the event reader. This means that clear now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Dec 4, 2022
@tim-blackbird
Copy link
Contributor

Not necessarily against this change but this was done on purpose because the reader is useless after calling clear.

@mockersf
Copy link
Member

mockersf commented Dec 4, 2022

Currently, the method consumes the reader, which makes it unusable.

Could you add more details on how that's unusable? Once cleared, you can't use the event reader anymore, but then there are no events in it.

You'll have to fix other things (see CI failures), I think the fact that it was consumed was useful for something but can't remember what from the top of my head.

@james7132 james7132 added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Dec 4, 2022
@james7132
Copy link
Member

This still changes a public facing API which makes it a breaking change. @mvlabat do you mind writing up a short changelog and migration guide?

@JoJoJet
Copy link
Member

JoJoJet commented Dec 5, 2022

Not necessarily against this change but this was done on purpose because the reader is useless after calling clear.

Could you add more details on how that's unusable? Once cleared, you can't use the event reader anymore, but then there are no events in it.

Consuming the event reader means it is not possible to do something like this this:

if my_condition {
    event_reader.clear();
}
for event in event_reader.iter() {
    ...
}

The EventReader is basically just a cursor -- it doesn't have ownership of anything, so I don't see what is gained by consuming it here.

@tim-blackbird
Copy link
Contributor

I can see how it could be annoying, hence the 'Not necessarily against this change'.
Personally, I would add a return or else there either way.

@cart
Copy link
Member

cart commented Dec 5, 2022

I think this is a reasonable change. I don't think we need a migration guide as this is a relaxing of constraints. Before, calling clear() would result in EventReader being dropped / no longer available. This means no code after it could use it. After the change, that code will behave identically: clear will still be the last usage of EventReader and it will be dropped. Functionally maybe there is some contrived case where that difference matters, but I can't come up with one.

@cart
Copy link
Member

cart commented Dec 5, 2022

I added a changelog entry though

@cart
Copy link
Member

cart commented Dec 5, 2022

bors r+

bors bot pushed a commit that referenced this pull request Dec 5, 2022
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader.
@bors
Copy link
Contributor

bors bot commented Dec 5, 2022

Build failed:

@cart
Copy link
Member

cart commented Dec 5, 2022

Welp the build found a non-trivial breaking change, so I was wrong this does need a migration guide. I'll fix that now.

@cart
Copy link
Member

cart commented Dec 5, 2022

bors r+

bors bot pushed a commit that referenced this pull request Dec 5, 2022
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader. 

## Migration Guide

`EventReader::clear` now takes a mutable reference instead of consuming the event reader. This means that `clear` now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

```rust
// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}
``` 

Co-authored-by: Carter Anderson <[email protected]>
@bors
Copy link
Contributor

bors bot commented Dec 5, 2022

Build failed:

@cart
Copy link
Member

cart commented Dec 5, 2022

bors r+

bors bot pushed a commit that referenced this pull request Dec 5, 2022
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader. 

## Migration Guide

`EventReader::clear` now takes a mutable reference instead of consuming the event reader. This means that `clear` now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

```rust
// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}
``` 

Co-authored-by: Carter Anderson <[email protected]>
@bors bors bot changed the title Borrow instead of consuming in EventReader::clear [Merged by Bors] - Borrow instead of consuming in EventReader::clear Dec 5, 2022
@bors bors bot closed this Dec 5, 2022
@james7132 james7132 added this to the 0.10 milestone Dec 6, 2022
ickshonpe pushed a commit to ickshonpe/bevy that referenced this pull request Dec 6, 2022
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader. 

## Migration Guide

`EventReader::clear` now takes a mutable reference instead of consuming the event reader. This means that `clear` now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

```rust
// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}
``` 

Co-authored-by: Carter Anderson <[email protected]>
@mvlabat
Copy link
Contributor Author

mvlabat commented Dec 6, 2022

Oh, that was merged sooner than I was able to reply! Thanks! :D

Just to add to @JoJoJet's comment, consuming the event reader also makes it more difficult to use inside structs that derive SystemParam.

@cart thank you for adding the change log and migration guide. I thought that the patch was quite trivial and didn't imply breaking changes, so I omitted them, but you were right to note that it requires specifying mutability in the arguments.

alradish pushed a commit to alradish/bevy that referenced this pull request Jan 22, 2023
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader. 

## Migration Guide

`EventReader::clear` now takes a mutable reference instead of consuming the event reader. This means that `clear` now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

```rust
// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}
``` 

Co-authored-by: Carter Anderson <[email protected]>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
The PR fixes the interface of `EventReader::clear`. Currently, the method consumes the reader, which makes it unusable.

## Changelog

- `EventReader::clear` now takes a mutable reference instead of consuming the event reader. 

## Migration Guide

`EventReader::clear` now takes a mutable reference instead of consuming the event reader. This means that `clear` now needs explicit mutable access to the reader variable, which previously could have been omitted in some cases:

```rust
// Old (0.9)
fn clear_events(reader: EventReader<SomeEvent>) {
  reader.clear();
}

// New (0.10)
fn clear_events(mut reader: EventReader<SomeEvent>) {
  reader.clear();
}
``` 

Co-authored-by: Carter Anderson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants