30 lines
737 B
Rust
30 lines
737 B
Rust
use std::{future::Future, ops::Deref, pin::Pin, sync::Arc};
|
|
|
|
pub struct LazyResolve(
|
|
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> + Send + Sync>,
|
|
);
|
|
|
|
impl LazyResolve {
|
|
pub fn new(
|
|
func: Box<
|
|
dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> + Send + Sync,
|
|
>,
|
|
) -> Self {
|
|
Self(Arc::new(func))
|
|
}
|
|
|
|
pub async fn call(&self) -> anyhow::Result<()> {
|
|
// Bad hack until .call becomes stable
|
|
(self.0)().await
|
|
}
|
|
}
|
|
|
|
impl Deref for LazyResolve {
|
|
type Target =
|
|
Arc<dyn Fn() -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> + Send + Sync>;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|