diff --git a/.gitignore b/.gitignore index 8b95939..8812abe 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ Cargo.lock callgrind* cache.out tarpaulin-report.html +.html *.bkp coverage/ testdata/ diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index f442d08..868db22 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -5,7 +5,7 @@ //! handling of the whole crafting process when the times comes #![allow(dead_code)] -use std::{path::Path, rc::Rc}; +use std::{path::Path, sync::Arc}; use more_fields::FieldsAsSlice; use rusqlite::Connection; @@ -148,9 +148,9 @@ where /// Type alias represinting the inner function parameter // #![feature(trait_alias)] -pub(super) type FilterFunctionType = dyn Fn(&T, &T) -> bool; +pub(super) type FilterFunctionType = dyn Fn(&T, &T) -> bool + Send + Sync; /// Type alias representing a filter -pub(super) type FilterType = Box>>; +pub(super) type FilterType = Arc>; #[derive(Clone)] /// A generic Filtering function diff --git a/weaponry/src/weaponry_structs/equipement_impl_advanced.rs b/weaponry/src/weaponry_structs/equipement_impl_advanced.rs index 6fa4647..89320ac 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_advanced.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_advanced.rs @@ -5,7 +5,7 @@ //! These impls are here to either prettify output, or allow for smoother implementation of other traits and inner workings use crate::interfaces::FilterFunction; -use std::{fmt::Debug, rc::Rc}; +use std::{fmt::Debug, sync::Arc}; impl Debug for FilterFunction { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -14,7 +14,7 @@ impl Debug for FilterFunction { } impl Default for FilterFunction { fn default() -> Self { - FilterFunction(Box::new(Rc::new(|_, _| true))) + FilterFunction(Arc::new(|_, _| true)) } } impl PartialEq for FilterFunction { @@ -32,6 +32,8 @@ mod test { fn test_filter_weapons() { let _ = format!("{:?}", FilterFunction::::default()); let r = FilterFunction::::new(&|_, _| false) == FilterFunction::default(); + + // const B: LazyLock<&'static dyn Fn(&InnerEquipementStats, &InnerEquipementStats) -> bool> = assert!(r); } } diff --git a/weaponry/src/weaponry_structs/equipement_impl_basics.rs b/weaponry/src/weaponry_structs/equipement_impl_basics.rs index c3228fb..c3ba8ad 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_basics.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_basics.rs @@ -8,25 +8,23 @@ //! Implementations to transform all Specialized equipement into a generic Equipement //! A specific implementation could be preferable, but this simplicity might allow for easier developpement -use comfy_table::{Table, modifiers::UTF8_ROUND_CORNERS}; -use std::fmt::Display; - -use crate::interfaces::{FilterFunction, Reportable}; - use super::{ Equipement, InitialEquipementList, equipement_advanced_repr::RefinedEquipementList, equipement_structs::{ - EqFinishing, EqMaterial, EqNature, EqShape, InnerEquipementStats, - filter_id_unique_and_right_category, + EqFinishing, EqMaterial, EqNature, EqShape, FILTER_TRUE, FILTER_UNIQUE, + FILTER_UNIQUE_AND_RIGHT_CATEGORY, InnerEquipementStats, }, }; +use crate::interfaces::Reportable; +use comfy_table::{Table, modifiers::UTF8_ROUND_CORNERS}; +use std::fmt::Display; impl From for Equipement { fn from(val: EqShape) -> Self { Self { stats: val.stats, - ..Default::default() + filter_function: FILTER_TRUE.clone(), } } } @@ -35,7 +33,7 @@ impl From for Equipement { fn from(val: EqMaterial) -> Self { Self { stats: val.stats, - filter_function: FilterFunction::new(filter_id_unique_and_right_category()), + filter_function: FILTER_UNIQUE_AND_RIGHT_CATEGORY.clone(), } } } @@ -45,7 +43,7 @@ impl From for Equipement { Self { stats: val.stats, // Checks that the nature is compatible with the previous iteration - filter_function: FilterFunction::new(filter_id_unique_and_right_category()), + filter_function: FILTER_UNIQUE_AND_RIGHT_CATEGORY.clone(), } } } @@ -54,7 +52,7 @@ impl From for Equipement { fn from(val: EqFinishing) -> Self { Self { stats: val.stats, - ..Default::default() + filter_function: FILTER_TRUE.clone(), } } } diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index ee6faa1..0015851 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -2,18 +2,16 @@ // File: weaponry/src/weaponry_structs/equipement_structs.rs //! Base equipement structures -use std::rc::Rc; - -use crate::interfaces::{FilterFunction, FilterFunctionType}; -use more_fields::FieldsAsSlice; -use serde::{Deserialize, Serialize}; - use super::{ DamageType, SpecialField, equipement_fields::{ EquipementSerializedCategory, EquipementTargets, WeaponBaseShape, WeaponBaseSize, }, }; +use crate::interfaces::{FilterFunction, FilterFunctionType}; +use more_fields::FieldsAsSlice; +use serde::{Deserialize, Serialize}; +use std::sync::{Arc, LazyLock}; /// The final equipement list /// @@ -70,9 +68,23 @@ impl FilterFunction { /// Generate and save a new filter, /// the use of a static reference allows for reuse of a generic filter pub fn new(filter: &'static FilterFunctionType) -> Self { - Self(Box::new(Rc::new(filter))) + Self(Arc::new(filter)) } } + +/// Filter letting every instance being merged +pub(crate) static FILTER_TRUE: LazyLock> = + LazyLock::new(|| FilterFunction::(Arc::new(|_, _| true))); +/// Filter letting the merge happen iff the id is unique (not present in the previous element) +/// and the category should allow the merge +pub(crate) static FILTER_UNIQUE_AND_RIGHT_CATEGORY: LazyLock> = + LazyLock::new(|| { + FilterFunction::(Arc::new(filter_id_unique_and_right_category())) + }); +/// Filter letting the merge happen iff the id is unique (not present in the previous element) +pub(crate) static FILTER_UNIQUE: LazyLock> = + LazyLock::new(|| FilterFunction::(Arc::new(filter_id_unique()))); + /// The inner statistics of the equipement, used across all parts and final weapon #[allow(clippy::missing_docs_in_private_items, missing_docs)] #[derive(Clone, Default, FieldsAsSlice, Debug)] diff --git a/weaponry/testgraph.html b/weaponry/testgraph.html deleted file mode 100644 index bdad448..0000000 --- a/weaponry/testgraph.html +++ /dev/null @@ -1,515 +0,0 @@ - - - - - Ravenous Balancing - - - - - -
-
-
- - - - \ No newline at end of file