pub trait Policy<E>: Send + Sync {
// Required method
fn execute<'life0, 'async_trait, F, Fut, T>(
&'life0 self,
f: F,
) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'async_trait>>
where F: Fn() -> Fut + Send + Sync + 'async_trait,
Fut: Future<Output = Result<T, E>> + Send + 'async_trait,
T: Send + 'async_trait,
Self: 'async_trait,
'life0: 'async_trait;
}Expand description
The core trait implemented by all resilience policies.
Policy<E> provides a uniform interface for wrapping async operations
with resilience patterns like retry, circuit breaker, timeout, etc.
§Type Parameter
E- The error type that the policy can produce
§Examples
Using a policy to execute an operation:
use do_over::{policy::Policy, retry::RetryPolicy};
use std::time::Duration;
let policy = RetryPolicy::fixed(3, Duration::from_millis(100));
let result = policy.execute(|| async {
// Your async operation here
Ok::<_, std::io::Error>("success")
}).await?;§Implementing Policy
To create a custom policy, implement this trait:
use do_over::policy::Policy;
use std::future::Future;
struct LoggingPolicy;
#[async_trait::async_trait]
impl<E: Send + Sync> Policy<E> for LoggingPolicy {
async fn execute<F, Fut, T>(&self, f: F) -> Result<T, E>
where
F: Fn() -> Fut + Send + Sync,
Fut: Future<Output = Result<T, E>> + Send,
T: Send,
{
println!("Executing operation...");
let result = f().await;
println!("Operation completed");
result
}
}Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.