Skip to content

Commit

Permalink
Gamepad type is Copy; do not require / return references to it in…
Browse files Browse the repository at this point in the history
… `Gamepads` API (#5296)

# Objective

- The `Gamepad` type is a tiny value-containing type that implements `Copy`.
- By convention, references to `Copy` types should be avoided, as they can introduce overhead and muddle the semantics of what's going on.
- This allows us to reduce boilerplate reference manipulation and lifetimes in user facing code.

## Solution

- Make assorted methods on `Gamepads` take / return a raw `Gamepad`, rather than `&Gamepad`.

## Migration Guide

- `Gamepads::iter` now returns an iterator of `Gamepad`. rather than an iterator of `&Gamepad`.
- `Gamepads::contains` now accepts a `Gamepad`, rather than a `&Gamepad`.
  • Loading branch information
alice-i-cecile committed Sep 3, 2022
1 parent 927441d commit 5849042
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
18 changes: 9 additions & 9 deletions crates/bevy_input/src/gamepad.rs
Expand Up @@ -47,23 +47,23 @@ pub struct Gamepads {

impl Gamepads {
/// Returns `true` if the `gamepad` is connected.
pub fn contains(&self, gamepad: &Gamepad) -> bool {
self.gamepads.contains(gamepad)
pub fn contains(&self, gamepad: Gamepad) -> bool {
self.gamepads.contains(&gamepad)
}

/// An iterator visiting all connected [`Gamepad`]s in arbitrary order.
pub fn iter(&self) -> impl Iterator<Item = &Gamepad> + '_ {
self.gamepads.iter()
/// Returns an iterator over registered [`Gamepad`]s in an arbitrary order.
pub fn iter(&self) -> impl Iterator<Item = Gamepad> + '_ {
self.gamepads.iter().copied()
}

/// Registers the `gamepad` marking it as connected.
fn register(&mut self, gamepad: Gamepad) {
self.gamepads.insert(gamepad);
}

/// Deregisters the `gamepad` marking it as disconnected.
fn deregister(&mut self, gamepad: &Gamepad) {
self.gamepads.remove(gamepad);
/// Deregisters the `gamepad`, marking it as disconnected.
fn deregister(&mut self, gamepad: Gamepad) {
self.gamepads.remove(&gamepad);
}
}

Expand Down Expand Up @@ -673,7 +673,7 @@ pub fn gamepad_connection_system(
info!("{:?} Connected", event.gamepad);
}
GamepadEventType::Disconnected => {
gamepads.deregister(&event.gamepad);
gamepads.deregister(event.gamepad);
info!("{:?} Disconnected", event.gamepad);
}
_ => (),
Expand Down
2 changes: 1 addition & 1 deletion examples/input/gamepad_input.rs
Expand Up @@ -15,7 +15,7 @@ fn gamepad_system(
button_axes: Res<Axis<GamepadButton>>,
axes: Res<Axis<GamepadAxis>>,
) {
for gamepad in gamepads.iter().cloned() {
for gamepad in gamepads.iter() {
if button_inputs.just_pressed(GamepadButton::new(gamepad, GamepadButtonType::South)) {
info!("{:?} just pressed South", gamepad);
} else if button_inputs.just_released(GamepadButton::new(gamepad, GamepadButtonType::South))
Expand Down

0 comments on commit 5849042

Please sign in to comment.