Skip to content

Commit bc863ce

Browse files
committed
Derived Copy trait for bevy_input events, Serialize/Deserialize for events in bevy_input and bevy_windows, PartialEq for events in both, and Eq where possible in both. (#6023)
# Objective Add traits to events in `bevy_input` and `bevy_windows`: `Copy`, `Serialize`/`Deserialize`, `PartialEq`, and `Eq`, as requested in #6022, #6023, #6024. ## Solution Added the traits to events in `bevy_input` and `bevy_windows`. Added dependency of `serde` in `Cargo.toml` of `bevy_input`. ## Migration Guide If one has been `.clone()`'ing `bevy_input` events, Clippy will now complain about that. Just remove `.clone()` to solve. ## Other Notes Some events in `bevy_input` had `f32` fields, so `Eq` trait was not derived for them. Some events in `bevy_windows` had `String` fields, so `Copy` trait was not derived for them. Co-authored-by: targrub <[email protected]>
1 parent 2b80a3f commit bc863ce

File tree

9 files changed

+69
-34
lines changed

9 files changed

+69
-34
lines changed

crates/bevy_input/src/gamepad.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Gamepads {
6868
}
6969

7070
/// The data contained in a [`GamepadEvent`] or [`GamepadEventRaw`].
71-
#[derive(Debug, Clone, PartialEq)]
71+
#[derive(Debug, Clone, Copy, PartialEq)]
7272
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
7373
pub enum GamepadEventType {
7474
/// A [`Gamepad`] has been connected.
@@ -101,7 +101,7 @@ pub enum GamepadEventType {
101101
/// [`Axis<GamepadAxis>`], and [`Axis<GamepadButton>`] resources won't be updated correctly.
102102
///
103103
/// An example for gamepad input mocking can be seen in the documentation of the [`GamepadEventRaw`].
104-
#[derive(Debug, Clone, PartialEq)]
104+
#[derive(Debug, Clone, Copy, PartialEq)]
105105
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
106106
pub struct GamepadEvent {
107107
/// The gamepad this event corresponds to.
@@ -204,7 +204,7 @@ impl GamepadEvent {
204204
/// #
205205
/// # bevy_ecs::system::assert_is_system(change_resource_on_gamepad_button_press);
206206
/// ```
207-
#[derive(Debug, Clone, PartialEq)]
207+
#[derive(Debug, Clone, Copy, PartialEq)]
208208
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
209209
pub struct GamepadEventRaw {
210210
/// The gamepad this event corresponds to.
@@ -703,7 +703,7 @@ pub fn gamepad_event_system(
703703
for event in raw_events.iter() {
704704
match event.event_type {
705705
GamepadEventType::Connected => {
706-
events.send(GamepadEvent::new(event.gamepad, event.event_type.clone()));
706+
events.send(GamepadEvent::new(event.gamepad, event.event_type));
707707
for button_type in &ALL_BUTTON_TYPES {
708708
let gamepad_button = GamepadButton::new(event.gamepad, *button_type);
709709
button_input.reset(gamepad_button);
@@ -714,7 +714,7 @@ pub fn gamepad_event_system(
714714
}
715715
}
716716
GamepadEventType::Disconnected => {
717-
events.send(GamepadEvent::new(event.gamepad, event.event_type.clone()));
717+
events.send(GamepadEvent::new(event.gamepad, event.event_type));
718718
for button_type in &ALL_BUTTON_TYPES {
719719
let gamepad_button = GamepadButton::new(event.gamepad, *button_type);
720720
button_input.reset(gamepad_button);

crates/bevy_input/src/keyboard.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use bevy_reflect::{FromReflect, Reflect};
1111
///
1212
/// The event is consumed inside of the [`keyboard_input_system`](crate::keyboard::keyboard_input_system)
1313
/// to update the [`Input<KeyCode>`](crate::Input<KeyCode>) resource.
14-
#[derive(Debug, Clone)]
14+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
1516
pub struct KeyboardInput {
1617
/// The scan code of the key.
1718
pub scan_code: u32,

crates/bevy_input/src/mouse.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use bevy_math::Vec2;
1010
///
1111
/// The event is read inside of the [`mouse_button_input_system`](crate::mouse::mouse_button_input_system)
1212
/// to update the [`Input<MouseButton>`](crate::Input<MouseButton>) resource.
13-
#[derive(Debug, Clone)]
13+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
1415
pub struct MouseButtonInput {
1516
/// The mouse button assigned to the event.
1617
pub button: MouseButton,
@@ -50,7 +51,8 @@ pub enum MouseButton {
5051
/// However, the event data does not make it possible to distinguish which device it is referring to.
5152
///
5253
/// [`DeviceEvent::MouseMotion`]: https://docs.rs/winit/latest/winit/event/enum.DeviceEvent.html#variant.MouseMotion
53-
#[derive(Debug, Clone)]
54+
#[derive(Debug, Clone, Copy, PartialEq)]
55+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
5456
pub struct MouseMotion {
5557
/// The change in the position of the pointing device since the last event was sent.
5658
pub delta: Vec2,
@@ -63,6 +65,7 @@ pub struct MouseMotion {
6365
/// The value of the event can either be interpreted as the amount of lines or the amount of pixels
6466
/// to scroll.
6567
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
68+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
6669
pub enum MouseScrollUnit {
6770
/// The line scroll unit.
6871
///
@@ -79,7 +82,8 @@ pub enum MouseScrollUnit {
7982
/// A mouse wheel event.
8083
///
8184
/// This event is the translated version of the `WindowEvent::MouseWheel` from the `winit` crate.
82-
#[derive(Debug, Clone)]
85+
#[derive(Debug, Clone, Copy, PartialEq)]
86+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
8387
pub struct MouseWheel {
8488
/// The mouse scroll unit.
8589
pub unit: MouseScrollUnit,

crates/bevy_internal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"]
4545
# Enable watching file system for asset hot reload
4646
filesystem_watcher = ["bevy_asset/filesystem_watcher"]
4747

48-
serialize = ["bevy_input/serialize"]
48+
serialize = ["bevy_input/serialize", "bevy_window/serialize"]
4949

5050
# Display server protocol support (X11 is enabled by default)
5151
wayland = ["bevy_winit/wayland"]

crates/bevy_reflect/src/impls/std.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use crate::{
99

1010
use crate::utility::{GenericTypeInfoCell, NonGenericTypeInfoCell};
1111
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_value};
12-
use bevy_utils::{Duration, HashMap, HashSet, Instant};
12+
use bevy_utils::{Duration, Instant};
13+
use bevy_utils::{HashMap, HashSet};
1314
use std::{
1415
any::Any,
1516
borrow::Cow,
@@ -889,18 +890,19 @@ mod tests {
889890
Enum, FromReflect, Reflect, ReflectSerialize, TypeInfo, TypeRegistry, Typed, VariantInfo,
890891
VariantType,
891892
};
892-
use bevy_utils::{HashMap, Instant};
893+
use bevy_utils::HashMap;
894+
use bevy_utils::{Duration, Instant};
893895
use std::f32::consts::{PI, TAU};
894896

895897
#[test]
896898
fn can_serialize_duration() {
897899
let mut type_registry = TypeRegistry::default();
898-
type_registry.register::<std::time::Duration>();
900+
type_registry.register::<Duration>();
899901

900902
let reflect_serialize = type_registry
901-
.get_type_data::<ReflectSerialize>(std::any::TypeId::of::<std::time::Duration>())
903+
.get_type_data::<ReflectSerialize>(std::any::TypeId::of::<Duration>())
902904
.unwrap();
903-
let _serializable = reflect_serialize.get_serializable(&std::time::Duration::ZERO);
905+
let _serializable = reflect_serialize.get_serializable(&Duration::ZERO);
904906
}
905907

906908
#[test]

crates/bevy_window/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ repository = "https://github.com/bevyengine/bevy"
88
license = "MIT OR Apache-2.0"
99
keywords = ["bevy"]
1010

11+
[features]
12+
default = []
13+
serialize = ["serde"]
14+
1115
[dependencies]
1216
# bevy
1317
bevy_app = { path = "../bevy_app", version = "0.9.0-dev" }
@@ -20,6 +24,7 @@ bevy_input = { path = "../bevy_input", version = "0.9.0-dev" }
2024
raw-window-handle = "0.4.2"
2125

2226
# other
27+
serde = { version = "1.0", features = ["derive"], optional = true }
2328

2429
[target.'cfg(target_arch = "wasm32")'.dependencies]
2530
web-sys = "0.3"

crates/bevy_window/src/cursor.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/// This `enum` is simply a copy of a similar `enum` found in [`winit`](https://docs.rs/winit/latest/winit/window/enum.CursorIcon.html).
55
/// `winit`, in turn, mostly copied cursor types available in the browser.
66
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
7+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
78
pub enum CursorIcon {
89
/// The platform-dependent default cursor.
910
Default,

crates/bevy_window/src/event.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use super::{WindowDescriptor, WindowId};
44
use bevy_math::{IVec2, Vec2};
55

66
/// A window event that is sent whenever a window's logical size has changed.
7-
#[derive(Debug, Clone)]
7+
#[derive(Debug, Clone, PartialEq)]
8+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
89
pub struct WindowResized {
910
pub id: WindowId,
1011
/// The new logical width of the window.
@@ -14,22 +15,25 @@ pub struct WindowResized {
1415
}
1516

1617
/// An event that indicates that a new window should be created.
17-
#[derive(Debug, Clone)]
18+
#[derive(Debug, Clone, PartialEq)]
19+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
1820
pub struct CreateWindow {
1921
pub id: WindowId,
2022
pub descriptor: WindowDescriptor,
2123
}
2224

2325
/// An event that indicates the window should redraw, even if its control flow is set to `Wait` and
2426
/// there have been no window events.
25-
#[derive(Debug, Clone)]
27+
#[derive(Debug, Clone, PartialEq, Eq)]
28+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
2629
pub struct RequestRedraw;
2730

2831
/// An event that is sent whenever a new window is created.
2932
///
3033
/// To create a new window, send a [`CreateWindow`] event - this
3134
/// event will be sent in the handler for that event.
32-
#[derive(Debug, Clone)]
35+
#[derive(Debug, Clone, PartialEq, Eq)]
36+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
3337
pub struct WindowCreated {
3438
pub id: WindowId,
3539
}
@@ -45,7 +49,8 @@ pub struct WindowCreated {
4549
/// [`WindowPlugin`]: crate::WindowPlugin
4650
/// [`Window`]: crate::Window
4751
/// [closing]: crate::Window::close
48-
#[derive(Debug, Clone)]
52+
#[derive(Debug, Clone, PartialEq, Eq)]
53+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
4954
pub struct WindowCloseRequested {
5055
pub id: WindowId,
5156
}
@@ -54,7 +59,8 @@ pub struct WindowCloseRequested {
5459
/// handler for [`Window::close`].
5560
///
5661
/// [`Window::close`]: crate::Window::close
57-
#[derive(Debug, Clone)]
62+
#[derive(Debug, Clone, PartialEq, Eq)]
63+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
5864
pub struct WindowClosed {
5965
pub id: WindowId,
6066
}
@@ -67,7 +73,8 @@ pub struct WindowClosed {
6773
///
6874
/// [`WindowEvent::CursorMoved`]: https://docs.rs/winit/latest/winit/event/enum.WindowEvent.html#variant.CursorMoved
6975
/// [`MouseMotion`]: bevy_input::mouse::MouseMotion
70-
#[derive(Debug, Clone)]
76+
#[derive(Debug, Clone, PartialEq)]
77+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
7178
pub struct CursorMoved {
7279
/// The identifier of the window the cursor has moved on.
7380
pub id: WindowId,
@@ -76,45 +83,51 @@ pub struct CursorMoved {
7683
pub position: Vec2,
7784
}
7885
/// An event that is sent whenever the user's cursor enters a window.
79-
#[derive(Debug, Clone)]
86+
#[derive(Debug, Clone, PartialEq, Eq)]
87+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
8088
pub struct CursorEntered {
8189
pub id: WindowId,
8290
}
8391
/// An event that is sent whenever the user's cursor leaves a window.
84-
#[derive(Debug, Clone)]
92+
#[derive(Debug, Clone, PartialEq, Eq)]
93+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
8594
pub struct CursorLeft {
8695
pub id: WindowId,
8796
}
8897

8998
/// An event that is sent whenever a window receives a character from the OS or underlying system.
90-
#[derive(Debug, Clone)]
99+
#[derive(Debug, Clone, PartialEq, Eq)]
100+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
91101
pub struct ReceivedCharacter {
92102
pub id: WindowId,
93103
pub char: char,
94104
}
95105

96106
/// An event that indicates a window has received or lost focus.
97-
#[derive(Debug, Clone)]
107+
#[derive(Debug, Clone, PartialEq, Eq)]
98108
pub struct WindowFocused {
99109
pub id: WindowId,
100110
pub focused: bool,
101111
}
102112

103113
/// An event that indicates a window's scale factor has changed.
104-
#[derive(Debug, Clone)]
114+
#[derive(Debug, Clone, PartialEq)]
115+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
105116
pub struct WindowScaleFactorChanged {
106117
pub id: WindowId,
107118
pub scale_factor: f64,
108119
}
109120
/// An event that indicates a window's OS-reported scale factor has changed.
110-
#[derive(Debug, Clone)]
121+
#[derive(Debug, Clone, PartialEq)]
122+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
111123
pub struct WindowBackendScaleFactorChanged {
112124
pub id: WindowId,
113125
pub scale_factor: f64,
114126
}
115127

116128
/// Events related to files being dragged and dropped on a window.
117-
#[derive(Debug, Clone)]
129+
#[derive(Debug, Clone, PartialEq, Eq)]
130+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
118131
pub enum FileDragAndDrop {
119132
DroppedFile { id: WindowId, path_buf: PathBuf },
120133

@@ -124,7 +137,8 @@ pub enum FileDragAndDrop {
124137
}
125138

126139
/// An event that is sent when a window is repositioned in physical pixels.
127-
#[derive(Debug, Clone)]
140+
#[derive(Debug, Clone, PartialEq, Eq)]
141+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
128142
pub struct WindowMoved {
129143
pub id: WindowId,
130144
pub position: IVec2,

crates/bevy_window/src/window.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use raw_window_handle::RawWindowHandle;
77
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Reflect, FromReflect)]
88
#[reflect_value(PartialEq, Hash)]
99
/// A unique ID for a [`Window`].
10+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
1011
pub struct WindowId(Uuid);
1112

1213
/// Presentation mode for a window.
@@ -24,6 +25,7 @@ pub struct WindowId(Uuid);
2425
/// or updated on a [`Window`](Window::set_present_mode).
2526
#[repr(C)]
2627
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
28+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
2729
#[doc(alias = "vsync")]
2830
pub enum PresentMode {
2931
/// Chooses FifoRelaxed -> Fifo based on availability.
@@ -95,7 +97,8 @@ impl Default for WindowId {
9597
/// Please note that if the window is resizable, then when the window is
9698
/// maximized it may have a size outside of these limits. The functionality
9799
/// required to disable maximizing is not yet exposed by winit.
98-
#[derive(Debug, Clone, Copy)]
100+
#[derive(Debug, Clone, Copy, PartialEq)]
101+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
99102
pub struct WindowResizeConstraints {
100103
pub min_width: f32,
101104
pub min_height: f32,
@@ -215,6 +218,7 @@ pub struct Window {
215218
/// Bevy apps don't interact with this `enum` directly. Instead, they should use the methods on [`Window`].
216219
/// This `enum` is meant for authors of windowing plugins. See the documentation on [`crate::WindowPlugin`] for more information.
217220
#[derive(Debug)]
221+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
218222
pub enum WindowCommand {
219223
/// Set the window's [`WindowMode`].
220224
SetWindowMode {
@@ -288,6 +292,7 @@ pub enum WindowCommand {
288292

289293
/// Defines the way a window is displayed.
290294
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
295+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
291296
pub enum WindowMode {
292297
/// Creates a window that uses the given size.
293298
Windowed,
@@ -745,7 +750,8 @@ impl Window {
745750
}
746751

747752
/// Defines where window should be placed at on creation.
748-
#[derive(Debug, Clone, Copy)]
753+
#[derive(Debug, Clone, Copy, PartialEq)]
754+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
749755
pub enum WindowPosition {
750756
/// The position will be set by the window manager.
751757
Automatic,
@@ -762,7 +768,8 @@ pub enum WindowPosition {
762768
}
763769

764770
/// Defines which monitor to use.
765-
#[derive(Debug, Clone, Copy)]
771+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
772+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
766773
pub enum MonitorSelection {
767774
/// Uses current monitor of the window.
768775
///
@@ -782,7 +789,8 @@ pub enum MonitorSelection {
782789
/// See [`examples/window/window_settings.rs`] for usage.
783790
///
784791
/// [`examples/window/window_settings.rs`]: https://github.com/bevyengine/bevy/blob/latest/examples/window/window_settings.rs
785-
#[derive(Resource, Debug, Clone)]
792+
#[derive(Resource, Debug, Clone, PartialEq)]
793+
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
786794
pub struct WindowDescriptor {
787795
/// The requested logical width of the window's client area.
788796
///

0 commit comments

Comments
 (0)