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; mod builder;
/// A structure holding a [`Maze`] and iteratively solving it with a provided [`Algorithm`]. /// A structure holding a [`Maze`] and iteratively solving it with a provided [`Algorithm`].
pub struct Executor<Algo> pub struct Executor {
where
Algo: Algorithm,
{
delay: Duration, delay: Duration,
maze: Maze, maze: Maze,
algorithm: Algo, algorithm: Box<dyn Algorithm>,
} }
impl<A> Executor<A> impl Executor {
where
A: Algorithm,
{
/// Constructor. /// Constructor.
fn new(maze: Maze, algorithm: A, delay: Duration) -> Self { fn new(maze: Maze, algorithm: Box<dyn Algorithm>, delay: Duration) -> Self {
Self { Self {
maze, maze,
algorithm, 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 where
A: Algorithm + 'static,
MS: BuildableMazeState, MS: BuildableMazeState,
F: Fn(ExecutorBuilder<Unprovided>) -> ExecutorBuilder<MS>, F: Fn(ExecutorBuilder<Unprovided>) -> ExecutorBuilder<MS>,
{ {
let operation = builder; let operation = builder;
let builder = (operation)(new_builder()); let builder = (operation)(new_builder());
let (maze, delay) = builder.build(); let (maze, delay) = builder.build();
let algorithm = Box::new(algorithm);
Self::new(maze, algorithm, delay) 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 where
F: Fn(DynExecutorBuilder) -> DynExecutorBuilder, 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. /// 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. /// Describe the state of the [`Maze`] parameter in the builder of an [`crate::Executor`]. Not ment to be implemented.
pub trait MazeState {} pub trait MazeState {}
pub trait BuildableMazeState: MazeState { pub trait BuildableMazeState: MazeState {
fn get(self) -> Maze; fn get(&self) -> Maze;
} }
pub struct Unprovided; pub struct Unprovided;
@ -30,8 +30,8 @@ pub mod maze_state {
impl MazeState for Provided {} impl MazeState for Provided {}
impl BuildableMazeState for Provided { impl BuildableMazeState for Provided {
fn get(self) -> Maze { fn get(&self) -> Maze {
self.maze self.maze.clone()
} }
} }
@ -51,7 +51,7 @@ pub mod maze_state {
impl MazeState for Generated {} impl MazeState for Generated {}
impl BuildableMazeState for Generated { impl BuildableMazeState for Generated {
fn get(self) -> Maze { fn get(&self) -> Maze {
self.generator.generate() 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)
}
}