Skip to content

Commit

Permalink
default() shorthand (#4071)
Browse files Browse the repository at this point in the history
Adds a `default()` shorthand for `Default::default()` ... because life is too short to constantly type `Default::default()`.

```rust
use bevy::prelude::*;

#[derive(Default)]
struct Foo {
  bar: usize,
  baz: usize,
}

// Normally you would do this:
let foo = Foo {
  bar: 10,
  ..Default::default()
};

// But now you can do this:
let foo = Foo {
  bar: 10,
  ..default()
};
```

The examples have been adapted to use `..default()`. I've left internal crates as-is for now because they don't pull in the bevy prelude, and the ergonomics of each case should be considered individually.
  • Loading branch information
cart committed Mar 1, 2022
1 parent caf6611 commit b6a647c
Show file tree
Hide file tree
Showing 64 changed files with 446 additions and 408 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_internal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pub use crate::{
app::prelude::*, asset::prelude::*, core::prelude::*, ecs::prelude::*, input::prelude::*,
log::prelude::*, math::prelude::*, reflect::prelude::*, scene::prelude::*,
transform::prelude::*, window::prelude::*, DefaultPlugins, MinimalPlugins,
transform::prelude::*, utils::prelude::*, window::prelude::*, DefaultPlugins, MinimalPlugins,
};

pub use bevy_derive::bevy_main;
Expand Down
30 changes: 30 additions & 0 deletions crates/bevy_utils/src/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// An ergonomic abbreviation for [`Default::default()`] to make initializing structs easier.
/// This is especially helpful when combined with ["struct update syntax"](https://doc.rust-lang.org/book/ch05-01-defining-structs.html#creating-instances-from-other-instances-with-struct-update-syntax).
/// ```
/// use bevy_utils::default;
///
/// #[derive(Default)]
/// struct Foo {
/// a: usize,
/// b: usize,
/// c: usize,
/// }
///
/// // Normally you would initialize a struct with defaults using "struct update syntax"
/// // combined with `Default::default()`. This example sets `Foo::bar` to 10 and the remaining
/// // values to their defaults.
/// let foo = Foo {
/// a: 10,
/// ..Default::default()
/// };
///
/// // But now you can do this, which is equivalent:
/// let foo = Foo {
/// a: 10,
/// ..default()
/// };
/// ```
#[inline]
pub fn default<T: Default>() -> T {
std::default::Default::default()
}
14 changes: 11 additions & 3 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
mod enum_variant_meta;
pub mod prelude {
pub use crate::default;
}

pub mod label;

mod default;
mod enum_variant_meta;

pub use ahash::AHasher;
pub use default::default;
pub use enum_variant_meta::*;
pub type Entry<'a, K, V> = hashbrown::hash_map::Entry<'a, K, V, RandomState>;
pub use hashbrown;
use hashbrown::hash_map::RawEntryMut;
pub use instant::{Duration, Instant};
pub use tracing;
pub use uuid::Uuid;

use ahash::RandomState;
use hashbrown::hash_map::RawEntryMut;
use std::{
fmt::Debug,
future::Future,
Expand All @@ -26,6 +32,8 @@ pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;

pub type Entry<'a, K, V> = hashbrown::hash_map::Entry<'a, K, V, RandomState>;

/// A hasher builder that will create a fixed hasher.
#[derive(Debug, Clone, Default)]
pub struct FixedState;
Expand Down
10 changes: 5 additions & 5 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ fn setup_contributor_selection(mut commands: Commands, asset_server: Res<AssetSe
custom_size: Some(Vec2::new(1.0, 1.0) * SPRITE_SIZE),
color: Color::hsla(hue, SATURATION_DESELECTED, LIGHTNESS_DESELECTED, ALPHA),
flip_x: flipped,
..Default::default()
..default()
},
texture: texture_handle.clone(),
transform,
..Default::default()
..default()
})
.id();

Expand All @@ -126,7 +126,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.insert_bundle(TextBundle {
style: Style {
align_self: AlignSelf::FlexEnd,
..Default::default()
..default()
},
text: Text {
sections: vec![
Expand All @@ -147,9 +147,9 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
},
],
..Default::default()
..default()
},
..Default::default()
..default()
});
}

Expand Down
4 changes: 2 additions & 2 deletions examples/2d/many_sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {
},
sprite: Sprite {
custom_size: Some(tile_size),
..Default::default()
..default()
},
..Default::default()
..default()
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/mesh2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn setup(
mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
transform: Transform::default().with_scale(Vec3::splat(128.)),
material: materials.add(ColorMaterial::from(Color::PURPLE)),
..Default::default()
..default()
});
}
2 changes: 1 addition & 1 deletion examples/2d/move_sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn_bundle(SpriteBundle {
texture: asset_server.load("branding/icon.png"),
transform: Transform::from_xyz(100., 0., 0.),
..Default::default()
..default()
})
.insert(Direction::Up);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/2d/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ fn setup(mut commands: Commands) {
sprite: Sprite {
color: Color::rgb(0.25, 0.25, 0.75),
custom_size: Some(Vec2::new(50.0, 50.0)),
..Default::default()
..default()
},
..Default::default()
..default()
});
}
10 changes: 5 additions & 5 deletions examples/2d/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn_bundle(SpriteBundle {
texture: ship_handle,
..Default::default()
..default()
})
.insert(Player {
movement_speed: 500.0, // metres per second
Expand All @@ -78,14 +78,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn_bundle(SpriteBundle {
texture: enemy_a_handle.clone(),
transform: Transform::from_xyz(0.0 - horizontal_margin, 0.0, 0.0),
..Default::default()
..default()
})
.insert(SnapToPlayer);
commands
.spawn_bundle(SpriteBundle {
texture: enemy_a_handle,
transform: Transform::from_xyz(0.0, 0.0 - vertical_margin, 0.0),
..Default::default()
..default()
})
.insert(SnapToPlayer);

Expand All @@ -94,7 +94,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn_bundle(SpriteBundle {
texture: enemy_b_handle.clone(),
transform: Transform::from_xyz(0.0 + horizontal_margin, 0.0, 0.0),
..Default::default()
..default()
})
.insert(RotateToPlayer {
rotation_speed: f32::to_radians(45.0), // degrees per second
Expand All @@ -103,7 +103,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn_bundle(SpriteBundle {
texture: enemy_b_handle,
transform: Transform::from_xyz(0.0, 0.0 + vertical_margin, 0.0),
..Default::default()
..default()
})
.insert(RotateToPlayer {
rotation_speed: f32::to_radians(90.0), // degrees per second
Expand Down
2 changes: 1 addition & 1 deletion examples/2d/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(SpriteBundle {
texture: asset_server.load("branding/icon.png"),
..Default::default()
..default()
});
}
4 changes: 2 additions & 2 deletions examples/2d/sprite_flipping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
flip_x: true,
// And don't flip it upside-down ( the default )
flip_y: false,
..Default::default()
..default()
},
..Default::default()
..default()
});
}
2 changes: 1 addition & 1 deletion examples/2d/sprite_sheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn setup(
.spawn_bundle(SpriteSheetBundle {
texture_atlas: texture_atlas_handle,
transform: Transform::from_scale(Vec3::splat(6.0)),
..Default::default()
..default()
})
.insert(AnimationTimer(Timer::from_seconds(0.1, true)));
}
6 changes: 3 additions & 3 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands
.spawn_bundle(Text2dBundle {
text: Text::with_section("translation", text_style.clone(), text_alignment),
..Default::default()
..default()
})
.insert(AnimateTranslation);
// Demonstrate changing rotation
commands
.spawn_bundle(Text2dBundle {
text: Text::with_section("rotation", text_style.clone(), text_alignment),
..Default::default()
..default()
})
.insert(AnimateRotation);
// Demonstrate changing scale
commands
.spawn_bundle(Text2dBundle {
text: Text::with_section("scale", text_style, text_alignment),
..Default::default()
..default()
})
.insert(AnimateScale);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/2d/texture_atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ fn setup(
transform: Transform {
translation: Vec3::new(150.0, 0.0, 0.0),
scale: Vec3::splat(4.0),
..Default::default()
..default()
},
sprite: TextureAtlasSprite::new(vendor_index),
texture_atlas: atlas_handle,
..Default::default()
..default()
});
// draw the atlas itself
commands.spawn_bundle(SpriteBundle {
texture: texture_atlas_texture,
transform: Transform::from_xyz(-300.0, 0.0, 0.0),
..Default::default()
..default()
});
}
10 changes: 5 additions & 5 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ fn setup(
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
..default()
});
// cube
commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
..default()
});
// light
commands.spawn_bundle(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
..Default::default()
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..Default::default()
..default()
});
// camera
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
..default()
});
}
Loading

0 comments on commit b6a647c

Please sign in to comment.