use crate::policy::RetryPolicy;
use crate::retry_result::RetryResult;
use crate::retryer::Retryer;
use crate::util;
use async_trait::async_trait;
#[async_trait]
pub trait Executor<T, E>: Send + Sync {
async fn execute(&self) -> RetryResult<T, E>;
fn prepare(&self) -> Retryer<T, E>
where
Self: Sized,
{
let pol = crate::global::get_default_policy();
Retryer {
policy: util::OwnedOrRef::Ref(pol),
count: 0,
function: Box::new(self),
}
}
async fn retry_with_policy(&self, policy: RetryPolicy) -> Result<T, E>
where
Self: Sized + 'static,
T: Send + Sync,
E: Send + Sync,
{
Retryer {
policy: util::OwnedOrRef::Owned(policy),
count: 0,
function: Box::new(self),
}
.run()
.await
}
fn retry_with_policy_ref<'a>(&'a self, policy: &'a RetryPolicy) -> Retryer<'a, T, E>
where
Self: Sized + 'static,
{
Retryer {
policy: util::OwnedOrRef::Ref(policy),
count: 0,
function: Box::new(self),
}
}
async fn retry_with_default_policy(&self) -> Result<T, E>
where
Self: Sized + 'static,
T: Send + Sync,
E: Send + Sync,
{
let pol = crate::global::get_default_policy();
Retryer {
policy: util::OwnedOrRef::Ref(pol),
count: 0,
function: Box::new(self),
}
.run()
.await
}
}
pub type AsyncFunction<'a, T, E> = Box<&'a dyn Executor<T, E>>;