From f3482203a4169fdbb2997185111d414e7d18f6e6 Mon Sep 17 00:00:00 2001 From: "Bruce Reif (Buswolley)" Date: Sat, 5 Mar 2022 10:55:24 -0800 Subject: [PATCH 1/5] fix some style nits from clippy --- src/data.rs | 10 +++++----- src/plugin.rs | 7 +------ src/prototype.rs | 2 +- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/data.rs b/src/data.rs index 5aca53c..3c42d73 100644 --- a/src/data.rs +++ b/src/data.rs @@ -112,13 +112,13 @@ impl ProtoData { let proto_map = self .handles .entry(protoytpe.name().to_string()) - .or_insert(HashMap::default()); + .or_insert_with(HashMap::default); let comp_map = proto_map .entry(component.type_id()) - .or_insert(HashMap::default()); + .or_insert_with(HashMap::default); let path_map = comp_map .entry(path.to_string()) - .or_insert(HashMap::default()); + .or_insert_with(HashMap::default); path_map.insert(T::TYPE_UUID, handle.clone_untyped()); } @@ -250,7 +250,7 @@ fn process_path( extensions: &Option>, deserializer: &Box, myself: &mut ProtoData, - directory: &String, + directory: &str, recursive: bool, ) { if let Ok(dir) = std::fs::read_dir(directory) { @@ -275,7 +275,7 @@ fn process_path( if let Some(filters) = &extensions { if let Some(ext) = path.extension().and_then(OsStr::to_str) { - if filters.iter().find(|filter| *filter == &ext).is_none() { + if !filters.iter().any(|filter| filter == &ext) { continue; } } diff --git a/src/plugin.rs b/src/plugin.rs index 1f607e9..a4769e1 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -5,6 +5,7 @@ use crate::{ prototype::{Prototype, Prototypical}, }; +#[derive(Default)] pub struct ProtoPlugin { pub options: Option, } @@ -121,12 +122,6 @@ impl ProtoPlugin { } } -impl Default for ProtoPlugin { - fn default() -> Self { - Self { options: None } - } -} - impl Plugin for ProtoPlugin { fn build(&self, app: &mut App) { if let Some(opts) = &self.options { diff --git a/src/prototype.rs b/src/prototype.rs index 0793e2f..a13f408 100644 --- a/src/prototype.rs +++ b/src/prototype.rs @@ -278,7 +278,7 @@ where { // Split string by commas // Allowing for: "A, B, C" to become [A, B, C] - Ok(v.split(",").map(|s| s.trim().to_string()).collect()) + Ok(v.split(',').map(|s| s.trim().to_string()).collect()) } fn visit_seq(self, seq: A) -> Result From 9355ed3969a803d39edb62e11f087fa1107dc7e6 Mon Sep 17 00:00:00 2001 From: "Bruce Reif (Buswolley)" Date: Sat, 5 Mar 2022 11:18:10 -0800 Subject: [PATCH 2/5] fix clippy borrowed box errors --- src/components.rs | 2 +- src/data.rs | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/components.rs b/src/components.rs index f4fa19e..182bbd3 100644 --- a/src/components.rs +++ b/src/components.rs @@ -8,5 +8,5 @@ use crate::prototype::Prototypical; pub trait ProtoComponent: Send + Sync + 'static { fn insert_self(&self, commands: &mut ProtoCommands, asset_server: &Res); #[allow(unused_variables)] - fn prepare(&self, world: &mut World, prototype: &Box, data: &mut ProtoData) {} + fn prepare(&self, world: &mut World, prototype: &dyn Prototypical, data: &mut ProtoData) {} } diff --git a/src/data.rs b/src/data.rs index 3c42d73..aae630a 100644 --- a/src/data.rs +++ b/src/data.rs @@ -60,8 +60,8 @@ impl ProtoData { /// * `name`: The name of the prototype /// /// returns: Option<&Prototype> - pub fn get_prototype(&self, name: &str) -> Option<&Box> { - self.prototypes.get(name) + pub fn get_prototype(&self, name: &str) -> Option<&dyn Prototypical> { + self.prototypes.get(name).map(|b| b.as_ref()) } /// Store a handle @@ -230,7 +230,7 @@ impl FromWorld for ProtoData { process_path( world, &options.extensions, - &options.deserializer, + options.deserializer.as_ref(), &mut myself, &directory, options.recursive_loading, @@ -248,7 +248,7 @@ impl FromWorld for ProtoData { fn process_path( world: &mut World, extensions: &Option>, - deserializer: &Box, + deserializer: &(dyn ProtoDeserializer + Send + Sync), myself: &mut ProtoData, directory: &str, recursive: bool, @@ -284,7 +284,7 @@ fn process_path( if let Ok(data) = std::fs::read_to_string(path) { if let Some(proto) = deserializer.deserialize(&data) { for component in proto.iter_components() { - component.prepare(world, &proto, myself); + component.prepare(world, proto.as_ref(), myself); } myself.prototypes.insert(proto.name().to_string(), proto); @@ -298,12 +298,12 @@ fn process_path( fn analyze_deps(data: &ProtoData) { // === Perform Analysis === // for proto in data.iter() { - check_for_cycles(proto, data, &mut IndexSet::default()); + check_for_cycles(proto.as_ref(), data, &mut IndexSet::default()); } // === Analysis Functions === // fn check_for_cycles<'a>( - proto: &'a Box, + proto: &'a dyn Prototypical, data: &'a ProtoData, traversed: &mut IndexSet<&'a str>, ) { From e8c788ed13cebfaa3d3f725f7f6eae5d468ee198 Mon Sep 17 00:00:00 2001 From: "Bruce Reif (Buswolley)" Date: Sat, 5 Mar 2022 11:22:49 -0800 Subject: [PATCH 3/5] fix clippy tabs in doc comments --- bevy_proto_derive/src/lib.rs | 4 ++-- examples/basic.rs | 2 +- src/data.rs | 24 ++++++++++++------------ src/prototype.rs | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/bevy_proto_derive/src/lib.rs b/bevy_proto_derive/src/lib.rs index ebd0c65..6085439 100644 --- a/bevy_proto_derive/src/lib.rs +++ b/bevy_proto_derive/src/lib.rs @@ -21,8 +21,8 @@ mod constants; /// /// #[derive(Clone, Serialize, Deserialize, ProtoComponent)] /// struct SomeComponent { -/// some_string: String, -/// some_int: i32, +/// some_string: String, +/// some_int: i32, /// } /// /// // Which generates: diff --git a/examples/basic.rs b/examples/basic.rs index 70464e4..53d26e7 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -37,7 +37,7 @@ impl ProtoComponent for Person { /// ``` /// #[derive(Clone, Serialize, Deserialize, Component, ProtoComponent)] /// struct Person { -/// pub name: String, +/// pub name: String, /// } /// ``` #[derive(Copy, Clone, Serialize, Deserialize, ProtoComponent, Component)] diff --git a/src/data.rs b/src/data.rs index aae630a..2b217e5 100644 --- a/src/data.rs +++ b/src/data.rs @@ -439,11 +439,11 @@ pub trait ProtoDeserializer: DynClone { /// // The default implementation: /// use bevy_proto::{Prototype, Prototypical}; /// fn example_deserialize(data: &str) -> Option> { - /// if let Ok(value) = serde_yaml::from_str::(data) { - /// Some(Box::new(value)) - /// } else { - /// None - /// } + /// if let Ok(value) = serde_yaml::from_str::(data) { + /// Some(Box::new(value)) + /// } else { + /// None + /// } /// } /// ``` fn deserialize(&self, data: &str) -> Option>; @@ -466,10 +466,10 @@ pub struct ProtoDataOptions { /// use bevy_proto::ProtoDataOptions; /// /// let opts = ProtoDataOptions { - /// directories: vec![String::from("assets/prototypes")], - /// recursive_loading: true, - /// extensions: Some(vec!["yaml"]), - /// ..Default::default() + /// directories: vec![String::from("assets/prototypes")], + /// recursive_loading: true, + /// extensions: Some(vec!["yaml"]), + /// ..Default::default() /// }; /// ``` pub recursive_loading: bool, @@ -486,9 +486,9 @@ pub struct ProtoDataOptions { /// use bevy_proto::ProtoDataOptions; /// /// let opts = ProtoDataOptions { - /// // Only allow .yaml or .json files - /// extensions: Some(vec!["yaml", "json"]), - /// ..Default::default() + /// // Only allow .yaml or .json files + /// extensions: Some(vec!["yaml", "json"]), + /// ..Default::default() /// }; /// ``` pub extensions: Option>, diff --git a/src/prototype.rs b/src/prototype.rs index a13f408..aaef005 100644 --- a/src/prototype.rs +++ b/src/prototype.rs @@ -124,7 +124,7 @@ pub trait Prototypical: 'static + Send + Sync { /// "#).unwrap(); /// /// // Get the EntityCommands for the player entity - /// let entity = commands.entity(player.single().0); + /// let entity = commands.entity(player.single().0); /// /// // Insert the new components /// let entity = proto.insert(entity, &data, &asset_server).id(); From 47ab0508067b6f99587f6f78a2c4800015d50c08 Mon Sep 17 00:00:00 2001 From: "Bruce Reif (Buswolley)" Date: Sat, 5 Mar 2022 11:24:42 -0800 Subject: [PATCH 4/5] fix unused import and one more box error --- src/data.rs | 2 +- src/prototype.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data.rs b/src/data.rs index 2b217e5..01aae6d 100644 --- a/src/data.rs +++ b/src/data.rs @@ -104,7 +104,7 @@ impl ProtoData { /// ``` pub fn insert_handle( &mut self, - protoytpe: &Box, + protoytpe: &dyn Prototypical, component: &dyn ProtoComponent, path: &HandlePath, handle: Handle, diff --git a/src/prototype.rs b/src/prototype.rs index aaef005..067fa31 100644 --- a/src/prototype.rs +++ b/src/prototype.rs @@ -4,7 +4,7 @@ use std::slice::Iter; use bevy::ecs::prelude::Commands; use bevy::ecs::system::EntityCommands; -use bevy::prelude::{AssetServer, Entity, Res}; +use bevy::prelude::{AssetServer, Res}; use indexmap::IndexSet; use serde::{ de::{self, Error, SeqAccess, Visitor}, From d7de2f9bcb974be7edc43de25934df294d62a924 Mon Sep 17 00:00:00 2001 From: "Bruce Reif (Buswolley)" Date: Sat, 5 Mar 2022 17:02:19 -0800 Subject: [PATCH 5/5] cleanup clippy --- src/data.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/data.rs b/src/data.rs index 01aae6d..8a31c43 100644 --- a/src/data.rs +++ b/src/data.rs @@ -26,6 +26,8 @@ impl Deref for HandlePath { } } +type UuidHandleMap = HashMap; + /// A resource containing data for all prototypes that need data stored pub struct ProtoData { /// Maps Prototype Name -> Component Type -> HandlePath -> Asset Type -> HandleUntyped @@ -35,10 +37,7 @@ pub struct ProtoData { TypeId, // Component Type HashMap< String, // Handle Path - HashMap< - Uuid, // Asset UUID - HandleUntyped, // Handle - >, + UuidHandleMap, >, >, >, @@ -46,7 +45,7 @@ pub struct ProtoData { } impl ProtoData { - pub fn new() -> Self { + pub fn empty() -> Self { Self { handles: HashMap::default(), prototypes: HashMap::default(), @@ -443,7 +442,7 @@ pub trait ProtoDeserializer: DynClone { /// Some(Box::new(value)) /// } else { /// None - /// } + /// } /// } /// ``` fn deserialize(&self, data: &str) -> Option>;