Reducing trait requirements

This commit is contained in:
vandomej 2021-10-06 10:54:34 -07:00
parent 789f8feef3
commit 71e613c1c1
5 changed files with 11 additions and 29 deletions

View file

@ -21,7 +21,7 @@ pub enum Error {
}
/// A wrapper around an object `T` that ties the object to a physical file
#[derive(Debug, PartialEq)]
#[derive(Debug)]
pub struct FileLinked<T>
where
T: Serialize,

View file

@ -13,18 +13,6 @@ pub struct TestState {
pub population: Vec<i64>,
}
impl Default for TestState {
fn default() -> Self {
let mut population: Vec<i64> = vec![];
for _ in 0..POPULATION_SIZE {
population.push(thread_rng().gen_range(0..10000))
}
TestState { population }
}
}
impl GeneticNode for TestState {
fn initialize() -> Result<Box<Self>, error::Error> {
let mut population: Vec<i64> = vec![];
@ -56,7 +44,7 @@ impl GeneticNode for TestState {
let mut v = self.population.clone();
v.sort();
v.sort_unstable();
v.reverse();
self.population = v[0..(POPULATION_REDUCTION_SIZE as usize)].to_vec();

View file

@ -6,12 +6,12 @@ use crate::error::Error;
use anyhow::Context;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Debug;
/// An enum used to control the state of a [`GeneticNode`]
///
/// [`GeneticNode`]: crate::bracket::genetic_node
#[derive(Clone, Debug, Serialize, Deserialize, Copy, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "enumType", content = "enumContent")]
pub enum GeneticState {
/// The node and it's data have not finished initializing
@ -52,10 +52,8 @@ pub trait GeneticNode {
/// Used externally to wrap a node implementing the [`GeneticNode`] trait. Processes state transitions for the given node as
/// well as signal recovery. Transition states are given by [`GeneticState`]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
pub struct GeneticNodeWrapper<T>
where
T: GeneticNode,
{
pub data: Option<T>,
state: GeneticState,
@ -64,7 +62,7 @@ where
impl<T> GeneticNodeWrapper<T>
where
T: GeneticNode + fmt::Debug,
T: GeneticNode + Debug,
{
/// Initializes a wrapper around a GeneticNode. If the initialization is successful the internal state will be changed to
/// `GeneticState::Simulate` otherwise it will remain as `GeneticState::Initialize` and will attempt to be created in
@ -114,7 +112,7 @@ where
pub fn process_node(&mut self, iterations: u64) -> Result<(), Error> {
// Looping through each state transition until the number of iterations have been reached.
loop {
match (self.state, &self.data) {
match (&self.state, &self.data) {
(GeneticState::Initialize, _) => {
self.iteration = 0;
let new_data = T::initialize()

View file

@ -22,7 +22,7 @@ use std::path::Path;
/// # Examples
///
/// TODO
#[derive(Clone, Serialize, Deserialize, Copy, Debug, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "enumType", content = "enumContent")]
pub enum IterationScaling {
/// Scales the number of simulations linearly with the height of the bracket tree given by `f(x) = mx` where
@ -38,10 +38,8 @@ impl Default for IterationScaling {
}
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[derive(Debug, Serialize, Deserialize)]
struct Bracket<T>
where
T: GeneticNode + Serialize,
{
tree: Option<Tree<Option<GeneticNodeWrapper<T>>>>,
iteration_scaling: IterationScaling,
@ -119,15 +117,14 @@ where
///
/// [`GeneticNode`]: genetic_node::GeneticNode
pub struct Gemla<T>
where
T: GeneticNode + Serialize + DeserializeOwned,
where T: Serialize
{
data: FileLinked<Bracket<T>>,
}
impl<T> Gemla<T>
where
T: GeneticNode + Serialize + DeserializeOwned + Default + Debug,
T: GeneticNode + Serialize + DeserializeOwned + Debug,
{
pub fn new(path: &Path, overwrite: bool) -> Result<Self, Error> {
match File::open(path) {

File diff suppressed because one or more lines are too long