Skip to content

Commit 2c6c0eb

Browse files
rparrettinodentry
authored andcommitted
Derive default for enums where possible (bevyengine#5158)
# Objective Fixes bevyengine#5153 ## Solution Search for all enums and manually check if they have default impls that can use this new derive. By my reckoning: | enum | num | |-|-| | total | 159 | | has default impl | 29 | | default is unit variant | 23 |
1 parent 871692b commit 2c6c0eb

File tree

14 files changed

+55
-175
lines changed

14 files changed

+55
-175
lines changed

crates/bevy_asset/src/handle.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,14 @@ where
109109
marker: PhantomData<fn() -> T>,
110110
}
111111

112+
// FIXME: Default is only needed because `Handle`'s field `handle_type` is currently ignored for reflection
113+
#[derive(Default)]
112114
enum HandleType {
115+
#[default]
113116
Weak,
114117
Strong(Sender<RefChange>),
115118
}
116119

117-
// FIXME: This only is needed because `Handle`'s field `handle_type` is currently ignored for reflection
118-
impl Default for HandleType {
119-
fn default() -> Self {
120-
Self::Weak
121-
}
122-
}
123-
124120
impl Debug for HandleType {
125121
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126122
match self {

crates/bevy_core_pipeline/src/clear_color.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@ use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
44
use bevy_render::{color::Color, extract_resource::ExtractResource};
55
use serde::{Deserialize, Serialize};
66

7-
#[derive(Reflect, Serialize, Deserialize, Clone, Debug)]
7+
#[derive(Reflect, Serialize, Deserialize, Clone, Debug, Default)]
88
#[reflect_value(Serialize, Deserialize)]
99
pub enum ClearColorConfig {
10+
#[default]
1011
Default,
1112
Custom(Color),
1213
None,
1314
}
1415

15-
impl Default for ClearColorConfig {
16-
fn default() -> Self {
17-
ClearColorConfig::Default
18-
}
19-
}
20-
2116
/// When used as a resource, sets the color that is used to clear the screen between frames.
2217
///
2318
/// This color appears as the "background" color for simple apps, when

crates/bevy_ecs/src/component.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,16 @@ mod sealed {
145145
/// #[component(storage = "SparseSet")]
146146
/// struct A;
147147
/// ```
148-
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
148+
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
149149
pub enum StorageType {
150150
/// Provides fast and cache-friendly iteration, but slower addition and removal of components.
151151
/// This is the default storage type.
152+
#[default]
152153
Table,
153154
/// Provides fast addition and removal of components, but slower iteration.
154155
SparseSet,
155156
}
156157

157-
impl Default for StorageType {
158-
fn default() -> Self {
159-
StorageType::Table
160-
}
161-
}
162-
163158
#[derive(Debug)]
164159
pub struct ComponentInfo {
165160
id: ComponentId,

crates/bevy_pbr/src/alpha.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use bevy_reflect::Reflect;
44

55
// FIXME: This should probably be part of bevy_render2!
66
/// Alpha mode
7-
#[derive(Component, Debug, Reflect, Copy, Clone, PartialEq)]
7+
#[derive(Component, Debug, Default, Reflect, Copy, Clone, PartialEq)]
88
#[reflect(Component, Default)]
99
pub enum AlphaMode {
10+
#[default]
1011
Opaque,
1112
/// An alpha cutoff must be supplied where alpha values >= the cutoff
1213
/// will be fully opaque and < will be fully transparent
@@ -15,9 +16,3 @@ pub enum AlphaMode {
1516
}
1617

1718
impl Eq for AlphaMode {}
18-
19-
impl Default for AlphaMode {
20-
fn default() -> Self {
21-
AlphaMode::Opaque
22-
}
23-
}

crates/bevy_reflect/bevy_reflect_derive/src/container_attributes.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const HASH_ATTR: &str = "Hash";
2424
pub(crate) const REFLECT_DEFAULT: &str = "ReflectDefault";
2525

2626
/// A marker for trait implementations registered via the `Reflect` derive macro.
27-
#[derive(Clone)]
27+
#[derive(Clone, Default)]
2828
pub(crate) enum TraitImpl {
2929
/// The trait is not registered as implemented.
30+
#[default]
3031
NotImplemented,
3132
/// The trait is registered as implemented.
3233
Implemented,
@@ -35,13 +36,6 @@ pub(crate) enum TraitImpl {
3536
/// The trait is registered with a custom function rather than an actual implementation.
3637
Custom(Ident),
3738
}
38-
39-
impl Default for TraitImpl {
40-
fn default() -> Self {
41-
Self::NotImplemented
42-
}
43-
}
44-
4539
/// A collection of traits that have been registered for a reflected type.
4640
///
4741
/// This keeps track of a few traits that are utilized internally for reflection

crates/bevy_reflect/bevy_reflect_derive/src/field_attributes.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ pub(crate) struct ReflectFieldAttr {
2222
}
2323

2424
/// Controls how the default value is determined for a field.
25+
#[derive(Default)]
2526
pub(crate) enum DefaultBehavior {
2627
/// Field is required.
28+
#[default]
2729
Required,
2830
/// Field can be defaulted using `Default::default()`.
2931
Default,
@@ -34,12 +36,6 @@ pub(crate) enum DefaultBehavior {
3436
Func(syn::ExprPath),
3537
}
3638

37-
impl Default for DefaultBehavior {
38-
fn default() -> Self {
39-
Self::Required
40-
}
41-
}
42-
4339
/// Parse all field attributes marked "reflect" (such as `#[reflect(ignore)]`).
4440
pub(crate) fn parse_field_attrs(attrs: &[Attribute]) -> Result<ReflectFieldAttr, syn::Error> {
4541
let mut args = ReflectFieldAttr::default();

crates/bevy_render/src/camera/camera.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,16 @@ impl RenderTarget {
307307
}
308308
}
309309

310-
#[derive(Debug, Clone, Copy, Reflect, Serialize, Deserialize)]
310+
#[derive(Debug, Clone, Copy, Default, Reflect, Serialize, Deserialize)]
311311
#[reflect_value(Serialize, Deserialize)]
312312
pub enum DepthCalculation {
313313
/// Pythagorean distance; works everywhere, more expensive to compute.
314+
#[default]
314315
Distance,
315316
/// Optimization for 2D; assuming the camera points towards -Z.
316317
ZDifference,
317318
}
318319

319-
impl Default for DepthCalculation {
320-
fn default() -> Self {
321-
DepthCalculation::Distance
322-
}
323-
}
324-
325320
pub fn camera_system<T: CameraProjection + Component>(
326321
mut window_resized_events: EventReader<WindowResized>,
327322
mut window_created_events: EventReader<WindowCreated>,

crates/bevy_render/src/mesh/shape/capsule.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ impl Default for Capsule {
3131
}
3232
}
3333

34-
#[derive(Debug, Clone, Copy)]
34+
#[derive(Debug, Default, Clone, Copy)]
3535
/// Manner in which UV coordinates are distributed vertically.
3636
pub enum CapsuleUvProfile {
3737
/// UV space is distributed by how much of the capsule consists of the hemispheres.
38+
#[default]
3839
Aspect,
3940
/// Hemispheres get UV space according to the ratio of latitudes to rings.
4041
Uniform,
@@ -43,12 +44,6 @@ pub enum CapsuleUvProfile {
4344
Fixed,
4445
}
4546

46-
impl Default for CapsuleUvProfile {
47-
fn default() -> Self {
48-
CapsuleUvProfile::Aspect
49-
}
50-
}
51-
5247
impl From<Capsule> for Mesh {
5348
#[allow(clippy::needless_range_loop)]
5449
fn from(capsule: Capsule) -> Self {

crates/bevy_render/src/render_asset.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,14 @@ pub trait RenderAsset: Asset {
3939
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>>;
4040
}
4141

42-
#[derive(Clone, Hash, Debug, PartialEq, Eq, SystemLabel)]
42+
#[derive(Clone, Hash, Debug, Default, PartialEq, Eq, SystemLabel)]
4343
pub enum PrepareAssetLabel {
4444
PreAssetPrepare,
45+
#[default]
4546
AssetPrepare,
4647
PostAssetPrepare,
4748
}
4849

49-
impl Default for PrepareAssetLabel {
50-
fn default() -> Self {
51-
Self::AssetPrepare
52-
}
53-
}
54-
5550
/// This plugin extracts the changed assets from the "app world" into the "render world"
5651
/// and prepares them for the GPU. They can then be accessed from the [`RenderAssets`] resource.
5752
///

crates/bevy_render/src/texture/image.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,14 @@ pub struct Image {
115115
/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
116116
/// [`ImageSampler::Default`], will read the sampler from the [`ImageSettings`] resource at runtime.
117117
/// Setting this to [`ImageSampler::Descriptor`] will override the global default descriptor for this [`Image`].
118-
#[derive(Debug, Clone)]
118+
#[derive(Debug, Default, Clone)]
119119
pub enum ImageSampler {
120120
/// Default image sampler, derived from the [`ImageSettings`] resource.
121+
#[default]
121122
Default,
122123
/// Custom sampler for this image which will override global default.
123124
Descriptor(wgpu::SamplerDescriptor<'static>),
124125
}
125-
impl Default for ImageSampler {
126-
fn default() -> Self {
127-
Self::Default
128-
}
129-
}
130126

131127
impl ImageSampler {
132128
/// Returns a sampler descriptor with `Linear` min and mag filters

0 commit comments

Comments
 (0)