Skip to content

Commit

Permalink
Derive default for enums where possible (bevyengine#5158)
Browse files Browse the repository at this point in the history
# 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 |
  • Loading branch information
rparrett authored and ItsDoot committed Feb 1, 2023
1 parent 04d1102 commit a34003e
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 175 deletions.
10 changes: 3 additions & 7 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,14 @@ where
marker: PhantomData<fn() -> T>,
}

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

// FIXME: This only is needed because `Handle`'s field `handle_type` is currently ignored for reflection
impl Default for HandleType {
fn default() -> Self {
Self::Weak
}
}

impl Debug for HandleType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_core_pipeline/src/clear_color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@ use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize};
use bevy_render::{color::Color, extract_resource::ExtractResource};
use serde::{Deserialize, Serialize};

#[derive(Reflect, Serialize, Deserialize, Clone, Debug)]
#[derive(Reflect, Serialize, Deserialize, Clone, Debug, Default)]
#[reflect_value(Serialize, Deserialize)]
pub enum ClearColorConfig {
#[default]
Default,
Custom(Color),
None,
}

impl Default for ClearColorConfig {
fn default() -> Self {
ClearColorConfig::Default
}
}

/// When used as a resource, sets the color that is used to clear the screen between frames.
///
/// This color appears as the "background" color for simple apps, when
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,16 @@ mod sealed {
/// #[component(storage = "SparseSet")]
/// struct A;
/// ```
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[derive(Debug, Copy, Clone, Default, Eq, PartialEq)]
pub enum StorageType {
/// Provides fast and cache-friendly iteration, but slower addition and removal of components.
/// This is the default storage type.
#[default]
Table,
/// Provides fast addition and removal of components, but slower iteration.
SparseSet,
}

impl Default for StorageType {
fn default() -> Self {
StorageType::Table
}
}

#[derive(Debug)]
pub struct ComponentInfo {
id: ComponentId,
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_pbr/src/alpha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use bevy_reflect::Reflect;

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

impl Eq for AlphaMode {}

impl Default for AlphaMode {
fn default() -> Self {
AlphaMode::Opaque
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const HASH_ATTR: &str = "Hash";
pub(crate) const REFLECT_DEFAULT: &str = "ReflectDefault";

/// A marker for trait implementations registered via the `Reflect` derive macro.
#[derive(Clone)]
#[derive(Clone, Default)]
pub(crate) enum TraitImpl {
/// The trait is not registered as implemented.
#[default]
NotImplemented,
/// The trait is registered as implemented.
Implemented,
Expand All @@ -35,13 +36,6 @@ pub(crate) enum TraitImpl {
/// The trait is registered with a custom function rather than an actual implementation.
Custom(Ident),
}

impl Default for TraitImpl {
fn default() -> Self {
Self::NotImplemented
}
}

/// A collection of traits that have been registered for a reflected type.
///
/// This keeps track of a few traits that are utilized internally for reflection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ pub(crate) struct ReflectFieldAttr {
}

/// Controls how the default value is determined for a field.
#[derive(Default)]
pub(crate) enum DefaultBehavior {
/// Field is required.
#[default]
Required,
/// Field can be defaulted using `Default::default()`.
Default,
Expand All @@ -34,12 +36,6 @@ pub(crate) enum DefaultBehavior {
Func(syn::ExprPath),
}

impl Default for DefaultBehavior {
fn default() -> Self {
Self::Required
}
}

/// Parse all field attributes marked "reflect" (such as `#[reflect(ignore)]`).
pub(crate) fn parse_field_attrs(attrs: &[Attribute]) -> Result<ReflectFieldAttr, syn::Error> {
let mut args = ReflectFieldAttr::default();
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,16 @@ impl RenderTarget {
}
}

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

impl Default for DepthCalculation {
fn default() -> Self {
DepthCalculation::Distance
}
}

pub fn camera_system<T: CameraProjection + Component>(
mut window_resized_events: EventReader<WindowResized>,
mut window_created_events: EventReader<WindowCreated>,
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_render/src/mesh/shape/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ impl Default for Capsule {
}
}

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

impl Default for CapsuleUvProfile {
fn default() -> Self {
CapsuleUvProfile::Aspect
}
}

impl From<Capsule> for Mesh {
#[allow(clippy::needless_range_loop)]
fn from(capsule: Capsule) -> Self {
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_render/src/render_asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@ pub trait RenderAsset: Asset {
) -> Result<Self::PreparedAsset, PrepareAssetError<Self::ExtractedAsset>>;
}

#[derive(Clone, Hash, Debug, PartialEq, Eq, SystemLabel)]
#[derive(Clone, Hash, Debug, Default, PartialEq, Eq, SystemLabel)]
pub enum PrepareAssetLabel {
PreAssetPrepare,
#[default]
AssetPrepare,
PostAssetPrepare,
}

impl Default for PrepareAssetLabel {
fn default() -> Self {
Self::AssetPrepare
}
}

/// This plugin extracts the changed assets from the "app world" into the "render world"
/// and prepares them for the GPU. They can then be accessed from the [`RenderAssets`] resource.
///
Expand Down
8 changes: 2 additions & 6 deletions crates/bevy_render/src/texture/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,14 @@ pub struct Image {
/// Used in [`Image`], this determines what image sampler to use when rendering. The default setting,
/// [`ImageSampler::Default`], will read the sampler from the [`ImageSettings`] resource at runtime.
/// Setting this to [`ImageSampler::Descriptor`] will override the global default descriptor for this [`Image`].
#[derive(Debug, Clone)]
#[derive(Debug, Default, Clone)]
pub enum ImageSampler {
/// Default image sampler, derived from the [`ImageSettings`] resource.
#[default]
Default,
/// Custom sampler for this image which will override global default.
Descriptor(wgpu::SamplerDescriptor<'static>),
}
impl Default for ImageSampler {
fn default() -> Self {
Self::Default
}
}

impl ImageSampler {
/// Returns a sampler descriptor with `Linear` min and mag filters
Expand Down
9 changes: 2 additions & 7 deletions crates/bevy_sprite/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ pub struct Sprite {

/// How a sprite is positioned relative to its [`Transform`](bevy_transform::components::Transform).
/// It defaults to `Anchor::Center`.
#[derive(Debug, Clone, Reflect)]
#[derive(Debug, Clone, Default, Reflect)]
#[doc(alias = "pivot")]
pub enum Anchor {
#[default]
Center,
BottomLeft,
BottomCenter,
Expand All @@ -38,12 +39,6 @@ pub enum Anchor {
Custom(Vec2),
}

impl Default for Anchor {
fn default() -> Self {
Anchor::Center
}
}

impl Anchor {
pub fn as_vec(&self) -> Vec2 {
match self {
Expand Down
23 changes: 8 additions & 15 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,32 @@ use smallvec::SmallVec;
/// Describes what type of input interaction has occurred for a UI node.
///
/// This is commonly queried with a `Changed<Interaction>` filter.
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
#[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)]
#[reflect_value(Component, Serialize, Deserialize, PartialEq)]
pub enum Interaction {
/// The node has been clicked
Clicked,
/// The node has been hovered over
Hovered,
/// Nothing has happened
#[default]
None,
}

impl Default for Interaction {
fn default() -> Self {
Interaction::None
}
}

/// Describes whether the node should block interactions with lower nodes
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
#[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)]
#[reflect_value(Component, Serialize, Deserialize, PartialEq)]
pub enum FocusPolicy {
/// Blocks interaction
#[default]
Block,
/// Lets interaction pass through
Pass,
}

impl Default for FocusPolicy {
fn default() -> Self {
FocusPolicy::Block
}
}

/// Contains entities whose Interaction should be set to None
#[derive(Default)]
pub struct State {
Expand Down
Loading

0 comments on commit a34003e

Please sign in to comment.