/rust/registry/src/index.crates.io-1949cf8c6b5b557f/prodash-31.0.0/src/tree/mod.rs
Line | Count | Source |
1 | | use crate::messages::MessageRingBuffer; |
2 | | |
3 | | /// The top-level of the progress tree. |
4 | | #[derive(Debug)] |
5 | | pub struct Root { |
6 | | pub(crate) inner: parking_lot::Mutex<Item>, |
7 | | } |
8 | | |
9 | | /// A `Tree` represents an element of the progress tree. |
10 | | /// |
11 | | /// It can be used to set progress and send messages. |
12 | | /// ```rust |
13 | | /// let tree = prodash::tree::Root::new(); |
14 | | /// let mut progress = tree.add_child("task 1"); |
15 | | /// |
16 | | /// progress.init(Some(10), Some("elements".into())); |
17 | | /// for p in 0..10 { |
18 | | /// progress.set(p); |
19 | | /// } |
20 | | /// progress.done("great success"); |
21 | | /// let mut sub_progress = progress.add_child_with_id("sub-task 1", *b"TSK2"); |
22 | | /// sub_progress.init(None, None); |
23 | | /// sub_progress.set(5); |
24 | | /// sub_progress.fail("couldn't finish"); |
25 | | /// ``` |
26 | | pub struct Item { |
27 | | pub(crate) key: crate::progress::Key, |
28 | | pub(crate) value: crate::progress::StepShared, |
29 | | pub(crate) highest_child_id: crate::progress::key::Id, |
30 | | pub(crate) tree: std::sync::Arc<HashMap<crate::progress::Key, crate::progress::Task>>, |
31 | | pub(crate) messages: std::sync::Arc<parking_lot::Mutex<MessageRingBuffer>>, |
32 | | } |
33 | | |
34 | | #[cfg(feature = "dashmap")] |
35 | | type HashMap<K, V> = dashmap::DashMap<K, V>; |
36 | | |
37 | | #[cfg(not(feature = "dashmap"))] |
38 | | type HashMap<K, V> = sync::HashMap<K, V>; |
39 | | |
40 | | #[cfg(not(feature = "dashmap"))] |
41 | | pub(crate) mod sync { |
42 | | pub struct HashMap<K, V>(parking_lot::Mutex<std::collections::HashMap<K, V>>); |
43 | | |
44 | | impl<K, V> HashMap<K, V> |
45 | | where |
46 | | K: Eq + std::hash::Hash, |
47 | | { |
48 | 0 | pub fn with_capacity(cap: usize) -> Self { |
49 | 0 | HashMap(parking_lot::Mutex::new(std::collections::HashMap::with_capacity(cap))) |
50 | 0 | } |
51 | 0 | pub fn extend_to(&self, out: &mut Vec<(K, V)>) |
52 | 0 | where |
53 | 0 | K: Clone, |
54 | 0 | V: Clone, |
55 | | { |
56 | 0 | let lock = self.0.lock(); |
57 | 0 | out.extend(lock.iter().map(|(k, v)| (k.clone(), v.clone()))) |
58 | 0 | } |
59 | 0 | pub fn remove(&self, key: &K) -> Option<V> { |
60 | 0 | self.0.lock().remove(key) |
61 | 0 | } |
62 | 0 | pub fn get<T>(&self, key: &K, cb: impl FnOnce(&V) -> T) -> Option<T> { |
63 | 0 | self.0.lock().get(key).map(cb) |
64 | 0 | } Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get::<[u8; 4], <prodash::tree::Item>::id::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get::<core::option::Option<prodash::unit::Unit>, <prodash::tree::Item>::unit::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get::<core::option::Option<usize>, <prodash::tree::Item>::max::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get::<alloc::string::String, <prodash::tree::Item>::message<alloc::string::String>::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get::<alloc::string::String, <prodash::tree::Item>::name::{closure#0}> |
65 | 0 | pub fn get_mut<T>(&self, key: &K, cb: impl FnOnce(&mut V) -> T) -> Option<T> { |
66 | 0 | self.0.lock().get_mut(key).map(cb) |
67 | 0 | } Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get_mut::<core::option::Option<usize>, <prodash::tree::Item>::set_max::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get_mut::<(), <prodash::tree::Item>::alter_progress<<prodash::tree::Item>::halted::{closure#0}>::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get_mut::<(), <prodash::tree::Item>::alter_progress<<prodash::tree::Item>::blocked::{closure#0}>::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get_mut::<(), <prodash::tree::Item>::alter_progress<<prodash::tree::Item>::running::{closure#0}>::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get_mut::<(), <prodash::tree::Item>::set_name<alloc::string::String>::{closure#0}>Unexecuted instantiation: <prodash::tree::sync::HashMap<prodash::progress::key::Key, prodash::progress::Task>>::get_mut::<(), <prodash::tree::Item>::init::{closure#0}> |
68 | 0 | pub fn insert(&self, key: K, value: V) { |
69 | 0 | self.0.lock().insert(key, value); |
70 | 0 | } |
71 | 0 | pub fn len(&self) -> usize { |
72 | 0 | self.0.lock().len() |
73 | 0 | } |
74 | 0 | pub fn clone(&self) -> Self |
75 | 0 | where |
76 | 0 | K: Clone, |
77 | 0 | V: Clone, |
78 | | { |
79 | 0 | HashMap(parking_lot::Mutex::new(self.0.lock().clone())) |
80 | 0 | } |
81 | | } |
82 | | } |
83 | | |
84 | | mod item; |
85 | | /// |
86 | | pub mod root; |
87 | | |
88 | | #[cfg(test)] |
89 | | mod tests; |