29
crates/cuddle-lazy/src/lib.rs
Normal file
29
crates/cuddle-lazy/src/lib.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user