1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::{
    future::IntoFuture,
    ops::{Deref, DerefMut},
};

use tokio::task::{JoinError, JoinHandle};

use crate::TransceiverError;

/// [Transceiver](crate::Transceiver) monitor
///
/// Collect [Transceiver](crate::Transceiver) transmitter or receiver thread handles
///
#[derive(Default, Debug)]
pub struct Monitor(Vec<JoinHandle<crate::Result<()>>>);
impl Monitor {
    /// Creates a new empty [Transceiver](crate::Transceiver) monitor
    pub fn new() -> Self {
        Default::default()
    }
    /// Joins all [Transceiver](crate::Transceiver) threads
    ///
    /// Instead you can `await` on [Monitor]s
    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)
    }
}