Skip to content

Commit

Permalink
small and mostly pointless refactoring (#2934)
Browse files Browse the repository at this point in the history
What is says on the tin.

This has got more to do with making `clippy` slightly more *quiet* than it does with changing anything that might greatly impact readability or performance.

that said, deriving `Default` for a couple of structs is a nice easy win
  • Loading branch information
danieleades committed Feb 13, 2022
1 parent 803e8cd commit d8974e7
Show file tree
Hide file tree
Showing 99 changed files with 329 additions and 304 deletions.
14 changes: 8 additions & 6 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,10 @@ impl App {
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self {
use std::any::TypeId;
if stage_label.type_id() == TypeId::of::<StartupStage>() {
panic!("add systems to a startup stage using App::add_startup_system_to_stage");
}
assert!(
stage_label.type_id() != TypeId::of::<StartupStage>(),
"add systems to a startup stage using App::add_startup_system_to_stage"
);
self.schedule.add_system_to_stage(stage_label, system);
self
}
Expand Down Expand Up @@ -407,9 +408,10 @@ impl App {
system_set: SystemSet,
) -> &mut Self {
use std::any::TypeId;
if stage_label.type_id() == TypeId::of::<StartupStage>() {
panic!("add system sets to a startup stage using App::add_startup_system_set_to_stage");
}
assert!(
stage_label.type_id() != TypeId::of::<StartupStage>(),
"add system sets to a startup stage using App::add_startup_system_set_to_stage"
);
self.schedule
.add_system_set_to_stage(stage_label, system_set);
self
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,11 @@ impl AssetServer {
});

// load asset dependencies and prepare asset type hashmap
for (label, loaded_asset) in load_context.labeled_assets.iter_mut() {
for (label, loaded_asset) in &mut load_context.labeled_assets {
let label_id = LabelId::from(label.as_ref().map(|label| label.as_str()));
let type_uuid = loaded_asset.value.as_ref().unwrap().type_uuid();
source_info.asset_types.insert(label_id, type_uuid);
for dependency in loaded_asset.dependencies.iter() {
for dependency in &loaded_asset.dependencies {
self.load_untracked(dependency.clone(), false);
}
}
Expand Down Expand Up @@ -484,7 +484,7 @@ impl AssetServer {

fn create_assets_in_load_context(&self, load_context: &mut LoadContext) {
let asset_lifecycles = self.server.asset_lifecycles.read();
for (label, asset) in load_context.labeled_assets.iter_mut() {
for (label, asset) in &mut load_context.labeled_assets {
let asset_value = asset
.value
.take()
Expand Down Expand Up @@ -674,7 +674,7 @@ mod test {
extensions == vec!["v1.2.3.pong", "2.3.pong", "3.pong", "pong"],
_ => false,
}
)
);
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,22 @@ impl<T: Asset> Assets<T> {
///
/// Keeps the allocated memory for reuse.
pub fn clear(&mut self) {
self.assets.clear()
self.assets.clear();
}

/// Reserves capacity for at least additional more elements to be inserted into the assets.
///
/// The collection may reserve more space to avoid frequent reallocations.
pub fn reserve(&mut self, additional: usize) {
self.assets.reserve(additional)
self.assets.reserve(additional);
}

/// Shrinks the capacity of the asset map as much as possible.
///
/// It will drop down as much as possible while maintaining the internal rules and possibly
/// leaving some space in accordance with the resize policy.
pub fn shrink_to_fit(&mut self) {
self.assets.shrink_to_fit()
self.assets.shrink_to_fit();
}

pub fn asset_event_system(
Expand All @@ -241,7 +241,7 @@ impl<T: Asset> Assets<T> {
// Check if the events are empty before calling `drain`.
// As `drain` triggers change detection.
if !assets.events.is_empty() {
events.send_batch(assets.events.drain())
events.send_batch(assets.events.drain());
}
}

Expand Down Expand Up @@ -331,6 +331,6 @@ mod tests {
let handle = assets_before.add(MyAsset);
app.add_asset::<MyAsset>(); // Ensure this doesn't overwrite the Asset
let assets_after = app.world.get_resource_mut::<Assets<MyAsset>>().unwrap();
assert!(assets_after.get(handle).is_some())
assert!(assets_after.get(handle).is_some());
}
}
15 changes: 9 additions & 6 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ impl<T: Asset> Handle<T> {
}

#[inline]
#[must_use]
pub fn clone_weak(&self) -> Self {
Handle::weak(self.id)
Self::weak(self.id)
}

pub fn clone_untyped(&self) -> HandleUntyped {
Expand Down Expand Up @@ -327,8 +328,9 @@ impl HandleUntyped {
}
}

pub fn clone_weak(&self) -> HandleUntyped {
HandleUntyped::weak(self.id)
#[must_use]
pub fn clone_weak(&self) -> Self {
Self::weak(self.id)
}

pub fn is_weak(&self) -> bool {
Expand All @@ -344,9 +346,10 @@ impl HandleUntyped {
/// The new handle will maintain the Strong or Weak status of the current handle.
pub fn typed<T: Asset>(mut self) -> Handle<T> {
if let HandleId::Id(type_uuid, _) = self.id {
if T::TYPE_UUID != type_uuid {
panic!("Attempted to convert handle to invalid type.");
}
assert!(
T::TYPE_UUID == type_uuid,
"Attempted to convert handle to invalid type."
);
}
let handle_type = match &self.handle_type {
HandleType::Strong(sender) => HandleType::Strong(sender.clone()),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/file_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) {
..
} = event
{
for path in paths.iter() {
for path in &paths {
if !changed.contains(path) {
let relative_path = path.strip_prefix(&asset_io.root_path).unwrap();
let _ = asset_server.load_untracked(relative_path.into(), true);
Expand Down
6 changes: 4 additions & 2 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ impl<T: Asset> LoadedAsset<T> {
self.dependencies.push(asset_path.to_owned());
}

#[must_use]
pub fn with_dependency(mut self, asset_path: AssetPath) -> Self {
self.add_dependency(asset_path);
self
}

#[must_use]
pub fn with_dependencies(mut self, mut asset_paths: Vec<AssetPath<'static>>) -> Self {
for asset_path in asset_paths.drain(..) {
self.add_dependency(asset_path);
Expand Down Expand Up @@ -132,7 +134,7 @@ impl<'a> LoadContext<'a> {

pub fn get_asset_metas(&self) -> Vec<AssetMeta> {
let mut asset_metas = Vec::new();
for (label, asset) in self.labeled_assets.iter() {
for (label, asset) in &self.labeled_assets {
asset_metas.push(AssetMeta {
dependencies: asset.dependencies.clone(),
label: label.clone(),
Expand Down Expand Up @@ -182,7 +184,7 @@ impl<T: AssetDynamic> AssetLifecycle for AssetLifecycleChannel<T> {
id,
version,
}))
.unwrap()
.unwrap();
} else {
panic!(
"Failed to downcast asset to {}.",
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_core/src/float_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ impl Hash for FloatOrd {
fn hash<H: Hasher>(&self, state: &mut H) {
if self.0.is_nan() {
// Ensure all NaN representations hash to the same value
state.write(bytemuck::bytes_of(&f32::NAN))
state.write(bytemuck::bytes_of(&f32::NAN));
} else if self.0 == 0.0 {
// Ensure both zeroes hash to the same value
state.write(bytemuck::bytes_of(&0.0f32))
state.write(bytemuck::bytes_of(&0.0f32));
} else {
state.write(bytemuck::bytes_of(&self.0));
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_core/src/time/fixed_timestep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl FixedTimestep {

/// Sets the label for the timestep. Setting a label allows a timestep
/// to be observed by the global [`FixedTimesteps`] resource.
#[must_use]
pub fn with_label(mut self, label: &str) -> Self {
self.state.label = Some(label.to_string());
self
Expand Down Expand Up @@ -197,7 +198,7 @@ impl System for FixedTimestep {
}

fn apply_buffers(&mut self, world: &mut World) {
self.internal_system.apply_buffers(world)
self.internal_system.apply_buffers(world);
}

fn initialize(&mut self, world: &mut World) {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl Timer {
self.stopwatch.reset();
self.finished = self.just_finished();
}
self.repeating = repeating
self.repeating = repeating;
}

/// Advance the timer by `delta` seconds.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ pub fn extract_clear_color(clear_color: Res<ClearColor>, mut render_world: ResMu
// If the clear color has changed
if clear_color.is_changed() {
// Update the clear color resource in the render world
render_world.insert_resource(clear_color.clone())
render_world.insert_resource(clear_color.clone());
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/main_pass_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Node for MainPass2dNode {

let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
for item in transparent_phase.items.iter() {
for item in &transparent_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_core_pipeline/src/main_pass_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl Node for MainPass3dNode {
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
for item in opaque_phase.items.iter() {
for item in &opaque_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Node for MainPass3dNode {
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
for item in alpha_mask_phase.items.iter() {
for item in &alpha_mask_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
Expand Down Expand Up @@ -154,7 +154,7 @@ impl Node for MainPass3dNode {
.begin_render_pass(&pass_descriptor);
let mut draw_functions = draw_functions.write();
let mut tracked_pass = TrackedRenderPass::new(render_pass);
for item in transparent_phase.items.iter() {
for item in &transparent_phase.items {
let draw_function = draw_functions.get_mut(item.draw_function).unwrap();
draw_function.draw(world, &mut tracked_pass, view_entity, item);
}
Expand Down
7 changes: 4 additions & 3 deletions crates/bevy_derive/src/bevy_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use syn::{parse_macro_input, ItemFn};

pub fn bevy_main(_attr: TokenStream, item: TokenStream) -> TokenStream {
let input = parse_macro_input!(item as ItemFn);
if input.sig.ident != "main" {
panic!("`bevy_main` can only be used on a function called 'main'.")
}
assert!(
input.sig.ident == "main",
"`bevy_main` can only be used on a function called 'main'.",
);

TokenStream::from(quote! {
#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ pub fn derive_app_label(input: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(input as syn::DeriveInput);
let mut trait_path = BevyManifest::default().get_path("bevy_app");
trait_path.segments.push(format_ident!("AppLabel").into());
derive_label(input, trait_path)
derive_label(input, &trait_path)
}
3 changes: 2 additions & 1 deletion crates/bevy_diagnostic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl Diagnostic {
"Diagnostic {:?} has name longer than {} characters, and so might overflow in the LogDiagnosticsPlugin\
Consider using a shorter name.",
name, MAX_DIAGNOSTIC_NAME_WIDTH
)
);
}
Diagnostic {
id,
Expand All @@ -77,6 +77,7 @@ impl Diagnostic {
}
}

#[must_use]
pub fn with_suffix(mut self, suffix: impl Into<Cow<'static, str>>) -> Self {
self.suffix = suffix.into();
self
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/macros/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn derive_component(input: TokenStream) -> TokenStream {
Err(e) => return e.into_compile_error().into(),
};

let storage = storage_path(bevy_ecs_path.clone(), attrs.storage);
let storage = storage_path(&bevy_ecs_path, attrs.storage);

ast.generics
.make_where_clause()
Expand Down Expand Up @@ -96,7 +96,7 @@ fn parse_component_attr(ast: &DeriveInput) -> Result<Attrs> {
Ok(attrs)
}

fn storage_path(bevy_ecs_path: Path, ty: StorageTy) -> TokenStream2 {
fn storage_path(bevy_ecs_path: &Path, ty: StorageTy) -> TokenStream2 {
let typename = match ty {
StorageTy::Table => Ident::new("TableStorage", Span::call_site()),
StorageTy::SparseSet => Ident::new("SparseStorage", Span::call_site()),
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ pub fn derive_system_label(input: TokenStream) -> TokenStream {
trait_path
.segments
.push(format_ident!("SystemLabel").into());
derive_label(input, trait_path)
derive_label(input, &trait_path)
}

#[proc_macro_derive(StageLabel)]
Expand All @@ -442,7 +442,7 @@ pub fn derive_stage_label(input: TokenStream) -> TokenStream {
let mut trait_path = bevy_ecs_path();
trait_path.segments.push(format_ident!("schedule").into());
trait_path.segments.push(format_ident!("StageLabel").into());
derive_label(input, trait_path)
derive_label(input, &trait_path)
}

#[proc_macro_derive(AmbiguitySetLabel)]
Expand All @@ -453,7 +453,7 @@ pub fn derive_ambiguity_set_label(input: TokenStream) -> TokenStream {
trait_path
.segments
.push(format_ident!("AmbiguitySetLabel").into());
derive_label(input, trait_path)
derive_label(input, &trait_path)
}

#[proc_macro_derive(RunCriteriaLabel)]
Expand All @@ -464,7 +464,7 @@ pub fn derive_run_criteria_label(input: TokenStream) -> TokenStream {
trait_path
.segments
.push(format_ident!("RunCriteriaLabel").into());
derive_label(input, trait_path)
derive_label(input, &trait_path)
}

pub(crate) fn bevy_ecs_path() -> syn::Path {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/archetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ impl Archetypes {
}

pub fn clear_entities(&mut self) {
for archetype in self.archetypes.iter_mut() {
for archetype in &mut self.archetypes {
archetype.clear_entities();
}
}
Expand Down
10 changes: 6 additions & 4 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ impl<'a, 'b> BundleInserter<'a, 'b> {
{
&mut *self.archetype
} else if new_archetype.id() == swapped_location.archetype_id {
&mut *new_archetype
new_archetype
} else {
// SAFE: the only two borrowed archetypes are above and we just did collision checks
&mut *self
Expand Down Expand Up @@ -630,9 +630,11 @@ unsafe fn initialize_bundle(
let mut deduped = component_ids.clone();
deduped.sort();
deduped.dedup();
if deduped.len() != component_ids.len() {
panic!("Bundle {} has duplicate components", bundle_type_name);
}
assert!(
deduped.len() == component_ids.len(),
"Bundle {} has duplicate components",
bundle_type_name
);

BundleInfo {
id,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub struct ComponentDescriptor {
impl ComponentDescriptor {
// SAFETY: The pointer points to a valid value of type `T` and it is safe to drop this value.
unsafe fn drop_ptr<T>(x: *mut u8) {
x.cast::<T>().drop_in_place()
x.cast::<T>().drop_in_place();
}

pub fn new<T: Component>() -> Self {
Expand Down
Loading

0 comments on commit d8974e7

Please sign in to comment.