From 603a1cd8b680a023b8d904e675d2d1dae6b7d23b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 11 Feb 2025 23:54:06 +0100 Subject: [PATCH 1/3] Cleanup requirements for impls --- weaponry/src/interfaces.rs | 54 ++--------- .../weaponry_structs/equipement_impl_logic.rs | 13 +-- .../weaponry_structs/equipement_impl_math.rs | 95 ------------------- 3 files changed, 12 insertions(+), 150 deletions(-) diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index cfa97cc..f7b169e 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -14,22 +14,18 @@ pub trait FromParts {} /// Trait implemented for the final collection of a recipe, allowing it to be combined with parts /// /// A recipe list will be combined with a part list to be updated and generate all combinaisons, so on and so forth -pub trait RecipeCollection { +pub trait RecipeCollection +where + for<'a> &'a <::Recipes as Combineable>::Inner: std::ops::Add, +{ /// The type of the inner recipes, this type must be combinable type Recipes: Combineable; - // /// Combine all current recipies with all the parts given - // #[deprecated] - // fn combine_all(&mut self, other: G) - // where - // G: RecipeCollection + std::marker::Sync; - /// Do the parrallel combination all inside fn combine_all_by_blanket(&mut self, other: &G) where G: RecipeCollection, { - // TODO: Test Add to Self instead self.assign_inner( self.ref_inner() .iter() @@ -46,23 +42,6 @@ pub trait RecipeCollection { ); } - // fn combine_all_by_ref(&mut self, other: G) - // where - // G: RecipeCollection + std::marker::Sync, - // { - // let mut future_receipes = vec![self.ref_inner().clone(); other.ref_inner().len()]; - // future_receipes - // .par_iter_mut() - // .zip(other.ref_inner()) - // .for_each(move |(a, b)| { - // a.par_iter_mut().for_each(move |e| { - // e.add_to_self(b); - // }); - // }); - - // self.inner = future_receipes.into_par_iter().flatten().collect(); - // } - /// Assign the inner value from an owned value, used for blanket implementations fn assign_inner(&mut self, inner: Vec); @@ -71,10 +50,6 @@ pub trait RecipeCollection { /// This allows to only clone when needed fn ref_inner(&self) -> &Vec; - // /// Gives access to a mutable reference of the inner list - // #[deprecated] - // fn refmut_inner(&mut self) -> &mut Vec; - // Optional filter ? /// Create a collection from a vector @@ -84,30 +59,19 @@ pub trait RecipeCollection { /// 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 Combineable { +pub trait Combineable +where + for<'a> &'a ::Inner: std::ops::Add, +{ /// The inner type of the combined elements, this type must implement Add // TODO : Make this help much more readable - type Inner: std::ops::Add + std::ops::AddAssign; + type Inner; /// Give access to a reference of the underlying data to be combined /// /// The designed combinaison method is std::ops::Add fn combinable_part(&self) -> &Self::Inner; - // /// Add an element to self and return a new value - // /// - // /// This method will clone it's own combinable part - // #[deprecated] - // fn add_to_other(&self, rhs: T) -> Self - // where - // T: Combineable; - - /// Add an element to a mutable self - // #[deprecated(note = "Mutate in place")] - fn add_to_self(&mut self, rhs: &T) - where - T: Combineable; - /// Add an element to self and generate a new Self /// /// As the signature implies, it will not mutate self not rhs and will generate a new value from the operation diff --git a/weaponry/src/weaponry_structs/equipement_impl_logic.rs b/weaponry/src/weaponry_structs/equipement_impl_logic.rs index ac25025..e2fed62 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_logic.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_logic.rs @@ -13,13 +13,6 @@ impl Combineable for Equipement { &self.stats } - fn add_to_self(&mut self, rhs: &T) - where - T: Combineable, - { - self.stats += rhs.combinable_part().clone() - } - fn add_ref_to_self(&self, rhs: &T) -> Self where T: Combineable, @@ -110,7 +103,7 @@ mod test { ..Default::default() }; assert_eq!( - i1 + i2, + &i1 + &i2, InnerStats { rarity: 1, integrity: 5, @@ -143,8 +136,8 @@ mod test { }, ); assert_eq!( - InnerStats::default() - + InnerStats { + &InnerStats::default() + + &InnerStats { damage_type: Some(DamageType(vec![ DamageMod::Secondary(PhysicalDamageType::Slash,), DamageMod::Primary(PhysicalDamageType::Blunt,), diff --git a/weaponry/src/weaponry_structs/equipement_impl_math.rs b/weaponry/src/weaponry_structs/equipement_impl_math.rs index 721d6dd..19d8235 100644 --- a/weaponry/src/weaponry_structs/equipement_impl_math.rs +++ b/weaponry/src/weaponry_structs/equipement_impl_math.rs @@ -6,57 +6,6 @@ use super::equipement_structs::InnerStats; use crate::weaponry_structs::SpecialCategory; -impl std::ops::Add for InnerStats { - type Output = Self; - - fn add(self, rhs: Self) -> Self::Output { - Self { - // TODO: Decide the behaviour of ids, as uuid are useless but this is even more useless - id: self.id, - name: { - let mut b = self.name.clone(); - b.append(&mut rhs.name.clone()); - b - }, - rarity: self.rarity + rhs.rarity, - cost: self.cost + rhs.cost, - difficulty: self.difficulty + rhs.difficulty, - tier: self.tier + rhs.tier, - damage: self.damage + rhs.damage, - p_damage: self.p_damage + rhs.p_damage, - precision: self.precision + rhs.precision, - rmax: self.rmax + rhs.rmax, - armor_break: self.armor_break + rhs.armor_break, - armor_pierce: self.armor_pierce + rhs.armor_pierce, - antimaterial: self.antimaterial + rhs.antimaterial, - damage_type: { - match (self.damage_type, rhs.damage_type) { - (None, None) => None, - (None, Some(a)) => Some(a), - (Some(a), None) => Some(a), - (Some(mut a), Some(mut b)) => { - a.0.append(&mut b.0); - Some(a) - } - } - }, - protection: self.protection + rhs.protection, - integrity: self.integrity + rhs.integrity, - torque_two_handed: self.torque_two_handed + rhs.torque_two_handed, - torque_one_handed: self.torque_one_handed + rhs.torque_one_handed, - dodge: self.dodge + rhs.dodge, - parry: self.parry + rhs.parry, - speed: self.speed + rhs.speed, - stealth: self.stealth + rhs.stealth, - category: self.category.or(rhs.category), - special: self.special + rhs.special, - action_cost: self.action_cost + rhs.action_cost, - reload_cost: self.reload_cost.or(rhs.reload_cost), - range: self.range + rhs.range, - agility_bonus: self.agility_bonus + rhs.agility_bonus, - } - } -} impl std::ops::Add for &InnerStats { type Output = InnerStats; @@ -109,50 +58,6 @@ impl std::ops::Add for &InnerStats { } } -impl std::ops::AddAssign for InnerStats { - fn add_assign(&mut self, rhs: InnerStats) { - // self.id; - // self.name.push('+'); - // self.name.push_str(&rhs.name); - self.rarity += rhs.rarity; - self.cost += rhs.cost; - self.difficulty += rhs.difficulty; - self.tier += rhs.tier; - self.damage += rhs.damage; - self.p_damage += rhs.p_damage; - self.precision += rhs.precision; - self.rmax += rhs.rmax; - self.armor_break += rhs.armor_break; - self.armor_pierce += rhs.armor_pierce; - self.antimaterial += rhs.antimaterial; - self.damage_type = { - match (self.damage_type.clone(), rhs.damage_type.clone()) { - (None, None) => None, - (None, Some(a)) => Some(a), - (Some(a), None) => Some(a), - (Some(mut a), Some(mut b)) => { - a.0.append(&mut b.0); - Some(a) - } - } - }; - self.protection += rhs.protection; - self.integrity += rhs.integrity; - self.torque_two_handed += rhs.torque_two_handed; - self.torque_one_handed += rhs.torque_one_handed; - self.dodge += rhs.dodge; - self.parry += rhs.parry; - self.speed += rhs.speed; - self.stealth += rhs.stealth; - self.category.clone().or(rhs.category.clone()); // Can be further optimized - // self.special += rhs.special.clone(); // Can be further optimized - self.action_cost += rhs.action_cost; - self.reload_cost.or(rhs.reload_cost); - self.range += rhs.range; - self.agility_bonus += rhs.agility_bonus; - } -} - impl std::ops::Add for SpecialCategory { type Output = Self; -- 2.45.1 From 9facac6b9f96e48e583def3c49e74dc5a1542b0a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 12 Feb 2025 00:36:49 +0100 Subject: [PATCH 2/3] Work on the Generic impls --- weaponry/src/interfaces.rs | 4 +- .../weaponry_structs/equipement_into_impls.rs | 30 ++++++++++ .../weaponry_structs/equipement_structs.rs | 56 +++++++++++++------ weaponry/src/weaponry_structs/mod.rs | 1 + 4 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 weaponry/src/weaponry_structs/equipement_into_impls.rs diff --git a/weaponry/src/interfaces.rs b/weaponry/src/interfaces.rs index f7b169e..d72a4cc 100644 --- a/weaponry/src/interfaces.rs +++ b/weaponry/src/interfaces.rs @@ -21,7 +21,9 @@ where /// The type of the inner recipes, this type must be combinable type Recipes: Combineable; - /// Do the parrallel combination all inside + /// 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_by_blanket(&mut self, other: &G) where G: RecipeCollection, diff --git a/weaponry/src/weaponry_structs/equipement_into_impls.rs b/weaponry/src/weaponry_structs/equipement_into_impls.rs new file mode 100644 index 0000000..3aae092 --- /dev/null +++ b/weaponry/src/weaponry_structs/equipement_into_impls.rs @@ -0,0 +1,30 @@ +// Workspace: weaponry +// File: weaponry/src/weaponry_structs/equipement_into_impls.rs +//! 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 super::{ + Equipement, + equipement_structs::{EqFinishing, EqMaterial, EqNature, EqShape}, +}; + +impl From for Equipement { + fn from(val: EqShape) -> Self { + Self { stats: val.stats } + } +} +impl From for Equipement { + fn from(val: EqMaterial) -> Self { + Self { stats: val.stats } + } +} +impl From for Equipement { + fn from(val: EqNature) -> Self { + Self { stats: val.stats } + } +} +impl From for Equipement { + fn from(val: EqFinishing) -> Self { + Self { stats: val.stats } + } +} diff --git a/weaponry/src/weaponry_structs/equipement_structs.rs b/weaponry/src/weaponry_structs/equipement_structs.rs index 33624e3..856ff94 100644 --- a/weaponry/src/weaponry_structs/equipement_structs.rs +++ b/weaponry/src/weaponry_structs/equipement_structs.rs @@ -59,21 +59,6 @@ pub struct InnerStats { pub(super) agility_bonus: u8, } -// /// A type representing a weapon reload potential -// #[derive(Clone, Default)] -// #[cfg_attr(test, derive(Debug, PartialEq))] -// pub struct OptionnalReloadType(Option); - -// /// Optionnal Weapon -// #[derive(Clone, Default)] -// #[cfg_attr(test, derive(Debug, PartialEq))] -// pub struct OptionnalWeaponCategory(Option); - -// /// Optionnal damage type -// #[derive(Clone, Default)] -// #[cfg_attr(test, derive(Debug, PartialEq))] -// pub struct OptionnalDamageType(Option); - /// Shape of the equipement, first mandatory part of an equipement pub struct EqShape { /// The inner stats of the part @@ -92,6 +77,8 @@ 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, } @@ -106,7 +93,11 @@ pub struct EqFinishing { mod test { // use super::*; - use super::{Equipement, InnerStats}; + use crate::{interfaces::RecipeCollection, weaponry_structs::NatureTarget}; + + use super::{ + EqFinishing, EqMaterial, EqNature, EqShape, Equipement, FinalEquipementList, InnerStats, + }; fn list_equip() -> Vec { (0..10) @@ -130,7 +121,38 @@ mod test { } #[test] - fn test_creating_all_equipements() {} + fn test_creating_all_equipements() { + let shape = vec![ + EqShape { + stats: InnerStats::default(), + } + .into(), + ]; + let nature = vec![ + EqNature { + stats: InnerStats::default(), + target: NatureTarget::Armor, + } + .into(), + ]; + let material = vec![ + EqMaterial { + stats: InnerStats::default(), + } + .into(), + ]; + let finishing = vec![ + EqFinishing { + stats: InnerStats::default(), + } + .into(), + ]; + + let mut leq = FinalEquipementList::from_vectored(shape); + leq.combine_all_by_blanket(&FinalEquipementList::from_vectored(nature)); + leq.combine_all_by_blanket(&FinalEquipementList::from_vectored(material)); + leq.combine_all_by_blanket(&FinalEquipementList::from_vectored(finishing)); + } #[ignore] #[test] diff --git a/weaponry/src/weaponry_structs/mod.rs b/weaponry/src/weaponry_structs/mod.rs index 228a565..6f34bd1 100644 --- a/weaponry/src/weaponry_structs/mod.rs +++ b/weaponry/src/weaponry_structs/mod.rs @@ -20,6 +20,7 @@ mod equipement_impl_database; mod equipement_impl_io; mod equipement_impl_logic; mod equipement_impl_math; +mod equipement_into_impls; pub mod equipement_structs; // Republishes the most used symbols -- 2.45.1 From 5d514d3d331b718f1ece73fa4beb9c1ce965ba04 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 12 Feb 2025 00:59:13 +0100 Subject: [PATCH 3/3] Adds todo notes --- weaponry/src/weaponry_structs/equipement_into_impls.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/weaponry/src/weaponry_structs/equipement_into_impls.rs b/weaponry/src/weaponry_structs/equipement_into_impls.rs index 3aae092..5beefef 100644 --- a/weaponry/src/weaponry_structs/equipement_into_impls.rs +++ b/weaponry/src/weaponry_structs/equipement_into_impls.rs @@ -1,5 +1,10 @@ // Workspace: weaponry // File: weaponry/src/weaponry_structs/equipement_into_impls.rs +// TODO: At some point a trait might want to be used to use these into instead of calling it directly +// : Doing so would ensure all prerequists are met +// : Another way of doing so might be to instead use Into instead +// : Another way would be to add a filter field that would allow or not the combinaison and abstract all +// logic that would be into doing multi traits things //! Implementations to transform all Specialized equipement into a generic Equipement //! A specific implementation could be preferable, but this simplicity might allow for easier developpement -- 2.45.1