From 7a29c707bf6c79b3dab6e79048818836cdc26bbe Mon Sep 17 00:00:00 2001 From: Alice Cecile Date: Sat, 3 Sep 2022 20:08:54 +0000 Subject: [PATCH] `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`. --- crates/bevy_input/src/gamepad.rs | 22 +++++++++++----------- examples/input/gamepad_input.rs | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/crates/bevy_input/src/gamepad.rs b/crates/bevy_input/src/gamepad.rs index 3318347946..1a2734df0c 100644 --- a/crates/bevy_input/src/gamepad.rs +++ b/crates/bevy_input/src/gamepad.rs @@ -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 + '_ { - self.gamepads.iter() + /// Returns an iterator over registered [`Gamepad`]s in an arbitrary order. + pub fn iter(&self) -> impl Iterator + '_ { + self.gamepads.iter().copied() } - /// Registers the `gamepad` marking it as connected. + /// 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); } } @@ -154,7 +154,7 @@ impl GamepadEvent { /// button_inputs: ResMut>, /// ) { /// 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); /// } @@ -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); } _ => (), diff --git a/examples/input/gamepad_input.rs b/examples/input/gamepad_input.rs index 9ad04ff2c8..10adf077e7 100644 --- a/examples/input/gamepad_input.rs +++ b/examples/input/gamepad_input.rs @@ -15,7 +15,7 @@ fn gamepad_system( button_axes: Res>, axes: Res>, ) { - 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))