use std::future::Future;
use std::pin::Pin;
pub struct Task<MSG> {
pub(crate) task: Pin<Box<dyn Future<Output = MSG>>>,
}
impl<MSG> Task<MSG>
where
MSG: 'static,
{
pub fn new<F>(f: F) -> Self
where
F: Future<Output = MSG> + 'static,
{
Self { task: Box::pin(f) }
}
pub fn map_msg<F, MSG2>(self, f: F) -> Task<MSG2>
where
F: Fn(MSG) -> MSG2 + 'static,
MSG2: 'static,
{
let task = self.task;
Task::new(async move {
let msg = task.await;
f(msg)
})
}
}
impl<F, MSG> From<F> for Task<MSG>
where
F: Future<Output = MSG> + 'static,
MSG: 'static,
{
fn from(f: F) -> Self {
Task::new(f)
}
}