1use crate::{
9 state::{Snapshotable, StateView, UserState},
10 Termination,
11};
12use num_traits::float::FloatCore;
13
14#[derive(Clone, Debug)]
16pub struct RunSummary<F> {
17 pub iterations: usize,
19
20 pub elapsed: std::time::Duration,
22
23 pub best_measure: Option<F>,
25}
26
27impl<F> RunSummary<F> {
28 pub(crate) fn new<S>(state: StateView<'_, S>) -> RunSummary<F>
29 where
30 S: UserState<Float = F>,
31 F: FloatCore,
32 {
33 Self {
34 iterations: state.iteration(),
35 elapsed: state.duration(),
36 best_measure: Some(state.best_measure()),
37 }
38 }
39}
40
41#[derive(Debug)]
45pub struct EngineOutput<R, S>
46where
47 S: UserState,
48{
49 pub result: R,
51
52 pub summary: RunSummary<S::Float>,
54
55 pub termination: Termination,
57}
58
59#[derive(Debug)]
63pub struct EngineOutputWithSnapshot<R, S>
64where
65 S: UserState + Snapshotable,
66{
67 pub result: R,
69
70 pub snapshot: S::Snapshot,
72
73 pub summary: RunSummary<S::Float>,
75
76 pub termination: Termination,
78}
79
80impl<R, S> EngineOutput<R, S>
81where
82 S: UserState,
83{
84 pub(crate) fn new(result: R, state: StateView<'_, S>, termination: Termination) -> Self {
85 let summary = RunSummary::new(state);
86 Self {
87 result,
88 summary,
89 termination,
90 }
91 }
92
93 pub fn with_snapshot(self, snapshot: S::Snapshot) -> EngineOutputWithSnapshot<R, S>
94 where
95 S: UserState + Snapshotable,
96 {
97 EngineOutputWithSnapshot {
98 result: self.result,
99 summary: self.summary,
100 termination: self.termination,
101 snapshot,
102 }
103 }
104}
105
106#[derive(thiserror::Error, Debug)]
107pub struct TrellisError<O, E> {
117 #[source]
118 pub error: E,
119
120 pub result: Option<O>,
122}
123
124impl<O, E: ::std::fmt::Debug> ::std::fmt::Display for TrellisError<O, E> {
125 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126 writeln!(f, "Trellis error: {:?}", self.error)
127 }
128}