diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index c78d5e3..8a05883 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -22,14 +22,14 @@ where for<'a> &'a <::Recipes as Combinable>::Inner: std::ops::Add, { /// The type of the inner recipes, this type must be combinable - type Recipes: Combinable; + type Recipes<'a>: Combinable<'a>; /// A blanket implementation giving all combinaisons of recipes with another RecipeCollection /// /// This method doesn't make use of parallel iterators as the workload is way faster than would be the memory dispatch - fn combine_all(&mut self, other: &G) + fn combine_all<'a, G>(&mut self, other: &G) where - G: RecipeCollection, + G: RecipeCollection = Self::Recipes<'a>>, { self.assign_inner( self.ref_inner() @@ -68,7 +68,7 @@ where /// Trait that ensure that two elements of a recipe are combinable, /// /// Being combinable means it can be coerced into elements that implement add -pub trait Combinable +pub trait Combinable<'a> where for<'a> &'a ::Inner: std::ops::Add, { @@ -102,13 +102,20 @@ where } /// Type alias represinting the inner function parameter +// #![feature(trait_alias)] pub(super) type FilterFunctionType = dyn Fn(&T, &T) -> bool; /// Type alias representing a filter -pub(super) type FilterType = Box>>; +pub(super) type FilterType<'a, T> = Box>>; #[derive(Clone)] /// A generic Filtering function -pub(crate) struct FilterFunction(pub(super) FilterType); +pub(crate) struct FilterFunction<'a, T>(pub(super) FilterType<'a, T>); + +// impl FilterFunction { +// fn new_from_closure(m: &FilterFunctionType) -> Self { +// FilterFunction(Box::new(Rc::new(m.to_owned()))) +// } +// } // /// A blanket implementation giving all combinaisons of recipes with another RecipeCollection // /// diff --git a/weaponry/src/weaponry_structs/equipement_impl_basics.rs b/weaponry/src/weaponry_structs/equipement_impl_basics.rs index 218b5f9..fc66e84 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_basics.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_basics.rs @@ -8,16 +8,18 @@ //! 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 crate::interfaces::FilterFunction; +use crate::interfaces::{FilterFunction, FilterType}; use std::fmt::Debug; use std::rc::Rc; use super::{ Equipement, - equipement_structs::{EqFinishing, EqMaterial, EqNature, EqShape}, + equipement_structs::{ + EqFinishing, EqMaterial, EqNature, EqShape, default_filter_for_equipement, + }, }; -impl From for Equipement { +impl From for Equipement<'_> { fn from(val: EqShape) -> Self { Self { stats: val.stats, @@ -25,7 +27,7 @@ impl From for Equipement { } } } -impl From for Equipement { +impl From for Equipement<'_> { fn from(val: EqMaterial) -> Self { Self { stats: val.stats, @@ -33,15 +35,15 @@ impl From for Equipement { } } } -impl From for Equipement { +impl From for Equipement<'_> { fn from(val: EqNature) -> Self { Self { stats: val.stats, - ..Default::default() + filter_function: FilterFunction(default_filter_for_equipement()), } } } -impl From for Equipement { +impl From for Equipement<'_> { fn from(val: EqFinishing) -> Self { Self { stats: val.stats, @@ -50,7 +52,7 @@ impl From for Equipement { } } -impl Debug for FilterFunction { +impl Debug for FilterFunction<'_, T> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, @@ -60,12 +62,12 @@ impl Debug for FilterFunction { ) } } -impl Default for FilterFunction { +impl Default for FilterFunction<'_, T> { fn default() -> Self { - FilterFunction(Box::new(Rc::new(|_, _| true))) + FilterFunction(Box::new(Rc::new(&|_, _| true))) } } -impl PartialEq for FilterFunction { +impl PartialEq for FilterFunction<'_, T> { fn eq(&self, _other: &Self) -> bool { true } diff --git a/weaponry/src/weaponry_structs/equipement_impl_logic.rs b/weaponry/src/weaponry_structs/equipement_impl_logic.rs index 21acb2e..a6c448f 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_logic.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_logic.rs @@ -6,7 +6,7 @@ use crate::interfaces::{Combinable, RecipeCollection}; use super::{Equipement, FinalEquipementList, equipement_structs::InnerStats}; -impl Combinable for Equipement { +impl Combinable for Equipement<'_> { type Inner = InnerStats; fn combinable_part(&self) -> &Self::Inner { @@ -31,8 +31,8 @@ impl Combinable for Equipement { } } -impl RecipeCollection for FinalEquipementList { - type Recipes = Equipement; +impl RecipeCollection for FinalEquipementList<'_> { + type Recipes<'a> = Equipement<'a>; fn from_vectored(list: Vec) -> Self { Self { inner: list } @@ -174,7 +174,7 @@ mod test { #[test] fn test_basic_logic() {} - impl Equipement { + impl Equipement<'_> { fn newish(name: &[&str], i: u8) -> Self { Equipement { stats: InnerStats { diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index c4ccdf8..1d31e98 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -6,7 +6,7 @@ use std::rc::Rc; use struct_field_names_as_array::FieldNamesAsArray; -use crate::interfaces::FilterFunction; +use crate::interfaces::{FilterFunction, FilterType}; use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory}; @@ -15,32 +15,37 @@ use super::{DamageType, NatureTarget, SpecialCategory, WeaponCategory}; /// This list is initialized with the base part and updated by succesive combinaisons #[derive(Clone, Debug)] #[cfg_attr(test, derive(PartialEq))] -pub struct FinalEquipementList { +pub struct FinalEquipementList<'a> { /// Inner List of final equipements - pub(super) inner: Vec, + pub(super) inner: Vec>, } /// The final equipement, obtained by combining all parts #[derive(Clone, Debug)] #[cfg_attr(test, derive(PartialEq))] -pub struct Equipement { +pub struct Equipement<'a> { /// The inner stats used by the equipement pub(super) stats: InnerStats, /// The filter, used to determine if adding is warranted - pub(super) filter_function: FilterFunction, + pub(super) filter_function: FilterFunction<'a, InnerStats>, } -impl Default for Equipement { +impl Default for Equipement<'_> { fn default() -> Self { Self { - filter_function: FilterFunction(Box::new(Rc::new(|self_, lhs| { - !self_.id.iter().any(|a| *a == lhs.id[0]) - }))), + filter_function: FilterFunction(default_filter_for_equipement()), stats: InnerStats::random_new(), } } } +/// The default filter function used by the equipements +pub(super) fn default_filter_for_equipement() -> FilterType<'static, InnerStats> { + Box::new(Rc::new(&|self_, lhs| { + !self_.id.iter().any(|a| *a == lhs.id[0]) + })) +} + /// 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, FieldNamesAsArray, Debug)] @@ -104,8 +109,6 @@ pub struct EqMaterial { pub struct EqNature { /// The inner stats of the part pub(super) stats: InnerStats, - // TODO: Be sure to take this into account - #[allow(dead_code)] /// Which kind of target this nature applies to pub(super) target: NatureTarget, } @@ -128,7 +131,7 @@ mod test { // use pretty_assertions::assert_eq; - fn list_equip() -> Vec { + fn list_equip() -> Vec> { (0..10) .map(|_| Equipement { ..Default::default()