use crate::{
state::{Snapshotable, StateView, UserState},
Termination,
};
use num_traits::float::FloatCore;
#[derive(Clone, Debug)]
pub struct RunSummary<F> {
pub iterations: usize,
pub elapsed: std::time::Duration,
pub best_measure: Option<F>,
}
impl<F> RunSummary<F> {
pub(crate) fn new<S>(state: StateView<'_, S>) -> RunSummary<F>
where
S: UserState<Float = F>,
F: FloatCore,
{
Self {
iterations: state.iteration(),
elapsed: state.duration(),
best_measure: Some(state.best_measure()),
}
}
}
#[derive(Debug)]
pub struct EngineOutput<R, S>
where
S: UserState,
{
pub result: R,
pub summary: RunSummary<S::Float>,
pub termination: Termination,
}
#[derive(Debug)]
pub struct EngineOutputWithSnapshot<R, S>
where
S: UserState + Snapshotable,
{
pub result: R,
pub snapshot: S::Snapshot,
pub summary: RunSummary<S::Float>,
pub termination: Termination,
}
impl<R, S> EngineOutput<R, S>
where
S: UserState,
{
pub(crate) fn new(result: R, state: StateView<'_, S>, termination: Termination) -> Self {
let summary = RunSummary::new(state);
Self {
result,
summary,
termination,
}
}
pub fn with_snapshot(self, snapshot: S::Snapshot) -> EngineOutputWithSnapshot<R, S>
where
S: UserState + Snapshotable,
{
EngineOutputWithSnapshot {
result: self.result,
summary: self.summary,
termination: self.termination,
snapshot,
}
}
}
#[derive(thiserror::Error, Debug)]
pub struct TrellisError<O, E> {
#[source]
pub error: E,
pub result: Option<O>,
}
impl<O, E: ::std::fmt::Debug> ::std::fmt::Display for TrellisError<O, E> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Trellis error: {:?}", self.error)
}
}