use std::{
future::IntoFuture,
ops::{Deref, DerefMut},
};
use tokio::task::{JoinError, JoinHandle};
use crate::TransceiverError;
#[derive(Default, Debug)]
pub struct Monitor(Vec<JoinHandle<crate::Result<()>>>);
impl Monitor {
pub fn new() -> Self {
Default::default()
}
pub async fn join(self) -> crate::Result<()> {
for h in self.0 {
let _ = h.await??;
}
Ok(())
}
}
impl Deref for Monitor {
type Target = Vec<JoinHandle<crate::Result<()>>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Monitor {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl IntoFuture for Monitor {
type Output = Result<Vec<Result<(), TransceiverError>>, JoinError>;
type IntoFuture = futures::future::TryJoinAll<JoinHandle<Result<(), TransceiverError>>>;
fn into_future(self) -> Self::IntoFuture {
futures::future::try_join_all(self.0)
}
}