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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::{
    collections::HashMap,
    ops::{Deref, DerefMut},
};

use crate::TaskHandle;

/// Map of tasks that can store TaskHandles based on any key type.
pub struct TaskMap<K, V> {
    inner: HashMap<K, TaskHandle<V>>,
}

impl<K, V> Deref for TaskMap<K, V> {
    type Target = HashMap<K, TaskHandle<V>>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}
impl<K, V> DerefMut for TaskMap<K, V> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

impl<K, V> TaskMap<K, V> {
    pub fn new() -> Self {
        Self {
            inner: HashMap::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use tokio::{sync::broadcast, time::sleep};

    use crate::TaskExt;

    use super::*;

    #[tokio::test]
    async fn is_dropped_correctly() {
        let (tx, _) = broadcast::channel(20);
        let mut map = TaskMap::new();
        let mut rx_clone = tx.subscribe();
        map.insert(
            1,
            tokio::spawn(async move { while let Ok(_) = rx_clone.recv().await {} })
                .to_task_handle(),
        );
        let mut rx_clone = tx.subscribe();
        map.insert(
            2,
            tokio::spawn(async move { while let Ok(_) = rx_clone.recv().await {} })
                .to_task_handle(),
        );

        let r = tx.send(true);
        assert!(r.is_ok());
        drop(map);
        // i guess we need to wait until tokio runtime drops inner task
        sleep(Duration::from_millis(1)).await;
        let r = tx.send(false);
        assert!(r.is_err(), "expected error, but got ok");
    }
}