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 {
/// 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.
/// 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<Input<GamepadButton>>,
/// ) {
/// 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);
}
_ => (),

View file

@ -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))