trellis_runner/
procedure.rs1use crate::CancellationGuard;
2
3use std::ops::Deref;
4
5pub trait Procedure<P> {
12 type Output;
19
20 type State;
21
22 const NAME: &'static str;
26 fn initialise(&self, _problem: &mut P, _state: &mut Self::State) {}
30 fn step(&self, problem: &mut P, state: &mut Self::State, guard: CancellationGuard<'_>);
32
33 fn finalise(&self, problem: &mut P, state: &Self::State) -> Self::Output;
35}
36
37pub trait FallibleProcedure<P> {
38 type Error: std::error::Error + 'static + Send + Sync;
39
40 type Output;
41
42 type State;
43
44 const NAME: &'static str;
48
49 fn initialise_fallible(
50 &self,
51 _problem: &mut P,
52 _state: &mut Self::State,
53 ) -> Result<(), Self::Error>;
54
55 fn step_fallible(
56 &self,
57 problem: &mut P,
58 state: &mut Self::State,
59 guard: CancellationGuard<'_>,
60 ) -> Result<(), Self::Error>;
61
62 fn finalise_fallible(
63 &self,
64 problem: &mut P,
65 state: &Self::State,
66 ) -> Result<Self::Output, Self::Error>;
67}
68
69pub struct Infallible<Proc>(pub Proc);
70
71impl<Proc> Deref for Infallible<Proc> {
72 type Target = Proc;
73 fn deref(&self) -> &Self::Target {
74 &self.0
75 }
76}
77
78impl<Proc, P> FallibleProcedure<P> for Infallible<Proc>
79where
80 Proc: Procedure<P>,
81{
82 const NAME: &'static str = <Proc as Procedure<P>>::NAME;
83 type State = <Proc as Procedure<P>>::State;
84 type Output = <Proc as Procedure<P>>::Output;
85 type Error = std::convert::Infallible;
86
87 fn initialise_fallible(
88 &self,
89 problem: &mut P,
90 state: &mut Self::State,
91 ) -> Result<(), Self::Error> {
92 let _: () = self.initialise(problem, state);
93 Ok(())
94 }
95
96 fn step_fallible(
97 &self,
98 problem: &mut P,
99 state: &mut Self::State,
100 guard: CancellationGuard<'_>,
101 ) -> Result<(), Self::Error> {
102 let _: () = self.step(problem, state, guard);
103 Ok(())
104 }
105
106 fn finalise_fallible(
107 &self,
108 problem: &mut P,
109 state: &Self::State,
110 ) -> Result<Self::Output, Self::Error> {
111 Ok(self.finalise(problem, state))
112 }
113}