use crate::error::AttemptFailure;
use super::attempt::Attempt;
pub(in crate::executor) struct ValueOperation<T, F> {
operation: F,
value: Option<T>,
}
impl<T, F> ValueOperation<T, F> {
pub(in crate::executor) fn new(operation: F) -> Self {
Self { operation, value: None }
}
pub(in crate::executor) fn into_value(self) -> T {
self.value.expect("retry loop succeeded without an operation value")
}
}
impl<T, E, F> Attempt<E> for ValueOperation<T, F>
where
F: FnMut() -> Result<T, E>,
{
fn call(&mut self) -> Result<(), AttemptFailure<E>> {
match (self.operation)() {
Ok(value) => {
self.value = Some(value);
Ok(())
}
Err(error) => Err(AttemptFailure::Error(error)),
}
}
}