Gamepad type is Copy; do not require / return references to it in 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`.
This commit is contained in:
Alice Cecile 2022-09-03 20:08:54 +00:00
parent 927441d048
commit 7a29c707bf
2 changed files with 12 additions and 12 deletions

View file

@ -47,23 +47,23 @@ pub struct Gamepads {
impl Gamepads { impl Gamepads {
/// Returns `true` if the `gamepad` is connected. /// Returns `true` if the `gamepad` is connected.
pub fn contains(&self, gamepad: &Gamepad) -> bool { pub fn contains(&self, gamepad: Gamepad) -> bool {
self.gamepads.contains(gamepad) self.gamepads.contains(&gamepad)
} }
/// An iterator visiting all connected [`Gamepad`]s in arbitrary order. /// Returns an iterator over registered [`Gamepad`]s in an arbitrary order.
pub fn iter(&self) -> impl Iterator<Item = &Gamepad> + '_ { pub fn iter(&self) -> impl Iterator<Item = Gamepad> + '_ {
self.gamepads.iter() self.gamepads.iter().copied()
} }
/// Registers the `gamepad` marking it as connected. /// Registers the `gamepad`, marking it as connected.
fn register(&mut self, gamepad: Gamepad) { fn register(&mut self, gamepad: Gamepad) {
self.gamepads.insert(gamepad); self.gamepads.insert(gamepad);
} }
/// Deregisters the `gamepad` marking it as disconnected. /// Deregisters the `gamepad`, marking it as disconnected.
fn deregister(&mut self, gamepad: &Gamepad) { fn deregister(&mut self, gamepad: Gamepad) {
self.gamepads.remove(gamepad); self.gamepads.remove(&gamepad);
} }
} }
@ -154,7 +154,7 @@ impl GamepadEvent {
/// button_inputs: ResMut<Input<GamepadButton>>, /// button_inputs: ResMut<Input<GamepadButton>>,
/// ) { /// ) {
/// let gamepad = gamepads.iter().next().unwrap(); /// let gamepad = gamepads.iter().next().unwrap();
/// let gamepad_button= GamepadButton::new(*gamepad, GamepadButtonType::South); /// let gamepad_button= GamepadButton::new(gamepad, GamepadButtonType::South);
/// ///
/// my_resource.0 = button_inputs.pressed(gamepad_button); /// my_resource.0 = button_inputs.pressed(gamepad_button);
/// } /// }
@ -673,7 +673,7 @@ pub fn gamepad_connection_system(
info!("{:?} Connected", event.gamepad); info!("{:?} Connected", event.gamepad);
} }
GamepadEventType::Disconnected => { GamepadEventType::Disconnected => {
gamepads.deregister(&event.gamepad); gamepads.deregister(event.gamepad);
info!("{:?} Disconnected", event.gamepad); info!("{:?} Disconnected", event.gamepad);
} }
_ => (), _ => (),

View file

@ -15,7 +15,7 @@ fn gamepad_system(
button_axes: Res<Axis<GamepadButton>>, button_axes: Res<Axis<GamepadButton>>,
axes: Res<Axis<GamepadAxis>>, 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)) { if button_inputs.just_pressed(GamepadButton::new(gamepad, GamepadButtonType::South)) {
info!("{:?} just pressed South", gamepad); info!("{:?} just pressed South", gamepad);
} else if button_inputs.just_released(GamepadButton::new(gamepad, GamepadButtonType::South)) } else if button_inputs.just_released(GamepadButton::new(gamepad, GamepadButtonType::South))