made executor dynamic and added dynamic builder

This commit is contained in:
JOLIMAITRE Matthieu 2022-05-30 15:04:44 +03:00
parent 618c6d57e8
commit c260404065
2 changed files with 47 additions and 18 deletions

View file

@ -92,21 +92,15 @@ impl<'m> Context<'m> {
mod builder;
/// A structure holding a [`Maze`] and iteratively solving it with a provided [`Algorithm`].
pub struct Executor<Algo>
where
Algo: Algorithm,
{
pub struct Executor {
delay: Duration,
maze: Maze,
algorithm: Algo,
algorithm: Box<dyn Algorithm>,
}
impl<A> Executor<A>
where
A: Algorithm,
{
impl Executor {
/// Constructor.
fn new(maze: Maze, algorithm: A, delay: Duration) -> Self {
fn new(maze: Maze, algorithm: Box<dyn Algorithm>, delay: Duration) -> Self {
Self {
maze,
algorithm,
@ -114,22 +108,27 @@ where
}
}
pub fn build<'f, F, MS>(algorithm: A, builder: F) -> Self
pub fn build<'f, A, F, MS>(algorithm: A, builder: F) -> Self
where
A: Algorithm + 'static,
MS: BuildableMazeState,
F: Fn(ExecutorBuilder<Unprovided>) -> ExecutorBuilder<MS>,
{
let operation = builder;
let builder = (operation)(new_builder());
let (maze, delay) = builder.build();
let algorithm = Box::new(algorithm);
Self::new(maze, algorithm, delay)
}
pub fn build_dyn<F>(algorithm: A, builder: F) -> Self
pub fn build_dyn<F>(algorithm: Box<dyn Algorithm>, builder: F) -> Self
where
F: Fn(DynExecutorBuilder) -> DynExecutorBuilder,
{
todo!()
let operation = builder;
let builder = (operation)(DynExecutorBuilder::new());
let (maze, delay) = builder.build();
Self::new(maze, algorithm, delay)
}
/// Submit the maze to the [`Algorithm`] and iteratively progress through the maze driven by said algorithm.

View file

@ -11,7 +11,7 @@ pub mod maze_state {
/// Describe the state of the [`Maze`] parameter in the builder of an [`crate::Executor`]. Not ment to be implemented.
pub trait MazeState {}
pub trait BuildableMazeState: MazeState {
fn get(self) -> Maze;
fn get(&self) -> Maze;
}
pub struct Unprovided;
@ -30,8 +30,8 @@ pub mod maze_state {
impl MazeState for Provided {}
impl BuildableMazeState for Provided {
fn get(self) -> Maze {
self.maze
fn get(&self) -> Maze {
self.maze.clone()
}
}
@ -51,7 +51,7 @@ pub mod maze_state {
impl MazeState for Generated {}
impl BuildableMazeState for Generated {
fn get(self) -> Maze {
fn get(&self) -> Maze {
self.generator.generate()
}
}
@ -120,4 +120,34 @@ impl<MS: BuildableMazeState> ExecutorBuilder<MS> {
}
}
pub struct DynExecutorBuilder {}
pub struct DynExecutorBuilder {
maze: Option<Box<dyn BuildableMazeState>>,
delay: Duration,
}
impl DynExecutorBuilder {
pub(crate) fn new() -> Self {
Self {
maze: None,
delay: Duration::from_millis(100),
}
}
pub fn maze(self, maze: Maze) -> Self {
todo!()
}
pub fn generated(self, maze: Box<dyn MazeGenerator>) -> Self {
todo!()
}
pub fn delay_ms(self, delay: u64) -> Self {
todo!()
}
pub(crate) fn build(self) -> (Maze, Duration) {
let maze = self.maze.expect("no buildable maze provided").get();
let delay = self.delay;
(maze, delay)
}
}