ravenous-tools/weaponry/src/interfaces.rs

137 lines
5.1 KiB
Rust

// Workspace: weaponry
// File: weaponry/src/interfaces.rs
//! Interface module holding all necesary traits
//! A good amount of effort is made to keep the code polymorphic enough to allow
//! handling of the whole crafting process when the times comes
#![allow(dead_code)]
// use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use std::rc::Rc;
/// This element can be converted to tex
pub trait ToTex {}
/// Specify this element can be made from parts (Unsure, maybe a way to ensure Add is presend ?)
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
where
for<'a> &'a <<Self as RecipeCollection>::Recipes as Combinable>::Inner: std::ops::Add,
{
/// The type of the inner recipes, this type must be combinable
type Recipes: Combinable;
/// 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<G>(&mut self, other: &G)
where
G: RecipeCollection<Recipes = Self::Recipes>,
{
self.assign_inner(
self.ref_inner()
.iter()
.flat_map(|inner_recipes| {
other
.ref_inner()
.iter()
.filter(|&external_recipes| external_recipes.backward_filter(inner_recipes))
.map(|external_recipes: &Self::Recipes| {
inner_recipes.add_ref_to_self(external_recipes)
})
.collect::<Vec<_>>()
})
.collect::<Vec<Self::Recipes>>(),
);
}
/// Assign the inner value from an owned value, used for blanket implementations
fn assign_inner(&mut self, inner: Vec<Self::Recipes>);
/// Gives access to a reference of the inner list
///
/// This allows to only clone when needed
fn ref_inner(&self) -> &Vec<Self::Recipes>;
/// Create a collection from a vector
fn from_vectored(list: Vec<Self::Recipes>) -> Self;
}
/// 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
where
for<'a> &'a <Self as Combinable>::Inner: std::ops::Add,
{
/// The inner type of the combined elements, this type must implement Add<&Inner>
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 generate a new Self
///
/// As the signature implies, it will not mutate self not rhs and will generate a new value from the operation
/// This pushes the actual requirement to &Inner : Add<&Inner>
fn add_ref_to_self<T>(&self, rhs: &T) -> Self
where
T: Combinable<Inner = Self::Inner>;
/// Filter to decide if the addition should be done
///
///
/// Please note that this comparison is done from the new element perspective,
/// asking it if it will allow itself to be added as There is as of now no forward filtering
/// Most filters will defer an internal FilterFunction into the Inner to do all inner working,
/// but the implementation specifics are left open
fn backward_filter<T>(&self, lhs: &T) -> bool
where
T: Combinable<Inner = Self::Inner>;
}
/// Type alias represinting the inner function parameter
// #![feature(trait_alias)]
pub(super) type FilterFunctionType<T> = dyn Fn(&T, &T) -> bool;
/// Type alias representing a filter
pub(super) type FilterType<T> = Box<Rc<FilterFunctionType<T>>>;
#[derive(Clone)]
/// A generic Filtering function
pub(crate) struct FilterFunction<T>(pub(super) FilterType<T>);
// impl<T> FilterFunction<T> {
// fn new_from_closure(m: &FilterFunctionType<T>) -> Self {
// FilterFunction(Box::new(Rc::new(m)))
// }
// }
// /// 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_par<G>(&mut self, other: &G)
// where
// G: RecipeCollection<Recipes = Self::Recipes> + std::marker::Sync,
// {
// self.assign_inner(
// self.owned_inner()
// .par_iter()
// .flat_map(|inner_recipes| {
// other
// .ref_inner()
// .iter()
// .filter(|&external_recipes| external_recipes.backward_filter(inner_recipes))
// .map(|external_recipes: &Self::Recipes| {
// inner_recipes.add_ref_to_self(external_recipes)
// })
// .collect::<Vec<_>>()
// })
// .collect::<Vec<Self::Recipes>>(),
// );
// }