pub struct TimerManager { /* private fields */ }
Expand description
A manager for controlling multiple timers.
Implementations§
Source§impl TimerManager
impl TimerManager
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new timer manager.
Examples found in repository?
examples/feature_showcase.rs (line 37)
36async fn main() {
37 let manager = TimerManager::new();
38
39 // 1. One-time Timer
40 let mut one_time_timer = Timer::new();
41 one_time_timer
42 .start_once(Duration::from_secs(2), OneTimeCallback)
43 .await
44 .unwrap();
45 manager.add_timer(one_time_timer);
46
47 // 2. Recurring Timer
48 let mut recurring_timer = Timer::new();
49 recurring_timer
50 .start_recurring(Duration::from_secs(3), RecurringCallback, Some(5))
51 .await
52 .unwrap();
53 let recurring_timer_id = manager.add_timer(recurring_timer);
54
55 // 3. Pause and Resume
56 sleep(Duration::from_secs(6)).await;
57 println!("Pausing recurring timer...");
58 if let Some(timer) = manager
59 .get_timer(recurring_timer_id)
60 .and_then(|t| t.lock().ok().map(|t| t.clone()))
61 {
62 timer.pause().await.unwrap();
63 }
64
65 sleep(Duration::from_secs(3)).await; // Wait while paused
66 println!("Resuming recurring timer...");
67 if let Some(timer) = manager
68 .get_timer(recurring_timer_id)
69 .and_then(|t| t.lock().ok().map(|t| t.clone()))
70 {
71 timer.resume().await.unwrap();
72 }
73
74 // 4. Dynamic Interval Adjustment
75 sleep(Duration::from_secs(6)).await;
76 println!("Adjusting recurring timer interval...");
77 if let Some(mut timer) = manager.get_timer(recurring_timer_id).and_then(|t: std::sync::Arc<std::sync::Mutex<Timer>>| t.lock().ok().map(|t| t.clone())) {
78 timer.adjust_interval(Duration::from_secs(1)).unwrap();
79 }
80
81 // 5. Timer Statistics
82 sleep(Duration::from_secs(10)).await;
83 println!("Retrieving timer statistics...");
84 if let Some(timer) = manager.get_timer(recurring_timer_id).and_then(|t| t.lock().ok().map(|t| t.clone())) {
85 let stats = timer.get_statistics().await;
86 println!("Timer statistics: {:?}", stats);
87 }
88
89 // 6. Error Handling
90 println!("Starting a timer with an error callback...");
91 let mut error_timer = Timer::new();
92 if let Err(e) = error_timer
93 .start_once(Duration::from_secs(2), ErrorCallback)
94 .await
95 {
96 println!("Error while starting timer: {}", e);
97 }
98
99 println!("All timers completed!");
100}
Sourcepub fn add_timer(&self, timer: Timer) -> u64
pub fn add_timer(&self, timer: Timer) -> u64
Adds a timer to the manager and returns its ID.
Examples found in repository?
examples/feature_showcase.rs (line 45)
36async fn main() {
37 let manager = TimerManager::new();
38
39 // 1. One-time Timer
40 let mut one_time_timer = Timer::new();
41 one_time_timer
42 .start_once(Duration::from_secs(2), OneTimeCallback)
43 .await
44 .unwrap();
45 manager.add_timer(one_time_timer);
46
47 // 2. Recurring Timer
48 let mut recurring_timer = Timer::new();
49 recurring_timer
50 .start_recurring(Duration::from_secs(3), RecurringCallback, Some(5))
51 .await
52 .unwrap();
53 let recurring_timer_id = manager.add_timer(recurring_timer);
54
55 // 3. Pause and Resume
56 sleep(Duration::from_secs(6)).await;
57 println!("Pausing recurring timer...");
58 if let Some(timer) = manager
59 .get_timer(recurring_timer_id)
60 .and_then(|t| t.lock().ok().map(|t| t.clone()))
61 {
62 timer.pause().await.unwrap();
63 }
64
65 sleep(Duration::from_secs(3)).await; // Wait while paused
66 println!("Resuming recurring timer...");
67 if let Some(timer) = manager
68 .get_timer(recurring_timer_id)
69 .and_then(|t| t.lock().ok().map(|t| t.clone()))
70 {
71 timer.resume().await.unwrap();
72 }
73
74 // 4. Dynamic Interval Adjustment
75 sleep(Duration::from_secs(6)).await;
76 println!("Adjusting recurring timer interval...");
77 if let Some(mut timer) = manager.get_timer(recurring_timer_id).and_then(|t: std::sync::Arc<std::sync::Mutex<Timer>>| t.lock().ok().map(|t| t.clone())) {
78 timer.adjust_interval(Duration::from_secs(1)).unwrap();
79 }
80
81 // 5. Timer Statistics
82 sleep(Duration::from_secs(10)).await;
83 println!("Retrieving timer statistics...");
84 if let Some(timer) = manager.get_timer(recurring_timer_id).and_then(|t| t.lock().ok().map(|t| t.clone())) {
85 let stats = timer.get_statistics().await;
86 println!("Timer statistics: {:?}", stats);
87 }
88
89 // 6. Error Handling
90 println!("Starting a timer with an error callback...");
91 let mut error_timer = Timer::new();
92 if let Err(e) = error_timer
93 .start_once(Duration::from_secs(2), ErrorCallback)
94 .await
95 {
96 println!("Error while starting timer: {}", e);
97 }
98
99 println!("All timers completed!");
100}
Sourcepub fn list_timers(&self) -> Vec<u64>
pub fn list_timers(&self) -> Vec<u64>
Lists all active timers.
Sourcepub fn get_timer(&self, id: u64) -> Option<Arc<Mutex<Timer>>>
pub fn get_timer(&self, id: u64) -> Option<Arc<Mutex<Timer>>>
Retrieves a timer by ID.
Examples found in repository?
examples/feature_showcase.rs (line 59)
36async fn main() {
37 let manager = TimerManager::new();
38
39 // 1. One-time Timer
40 let mut one_time_timer = Timer::new();
41 one_time_timer
42 .start_once(Duration::from_secs(2), OneTimeCallback)
43 .await
44 .unwrap();
45 manager.add_timer(one_time_timer);
46
47 // 2. Recurring Timer
48 let mut recurring_timer = Timer::new();
49 recurring_timer
50 .start_recurring(Duration::from_secs(3), RecurringCallback, Some(5))
51 .await
52 .unwrap();
53 let recurring_timer_id = manager.add_timer(recurring_timer);
54
55 // 3. Pause and Resume
56 sleep(Duration::from_secs(6)).await;
57 println!("Pausing recurring timer...");
58 if let Some(timer) = manager
59 .get_timer(recurring_timer_id)
60 .and_then(|t| t.lock().ok().map(|t| t.clone()))
61 {
62 timer.pause().await.unwrap();
63 }
64
65 sleep(Duration::from_secs(3)).await; // Wait while paused
66 println!("Resuming recurring timer...");
67 if let Some(timer) = manager
68 .get_timer(recurring_timer_id)
69 .and_then(|t| t.lock().ok().map(|t| t.clone()))
70 {
71 timer.resume().await.unwrap();
72 }
73
74 // 4. Dynamic Interval Adjustment
75 sleep(Duration::from_secs(6)).await;
76 println!("Adjusting recurring timer interval...");
77 if let Some(mut timer) = manager.get_timer(recurring_timer_id).and_then(|t: std::sync::Arc<std::sync::Mutex<Timer>>| t.lock().ok().map(|t| t.clone())) {
78 timer.adjust_interval(Duration::from_secs(1)).unwrap();
79 }
80
81 // 5. Timer Statistics
82 sleep(Duration::from_secs(10)).await;
83 println!("Retrieving timer statistics...");
84 if let Some(timer) = manager.get_timer(recurring_timer_id).and_then(|t| t.lock().ok().map(|t| t.clone())) {
85 let stats = timer.get_statistics().await;
86 println!("Timer statistics: {:?}", stats);
87 }
88
89 // 6. Error Handling
90 println!("Starting a timer with an error callback...");
91 let mut error_timer = Timer::new();
92 if let Err(e) = error_timer
93 .start_once(Duration::from_secs(2), ErrorCallback)
94 .await
95 {
96 println!("Error while starting timer: {}", e);
97 }
98
99 println!("All timers completed!");
100}
Trait Implementations§
Source§impl Default for TimerManager
impl Default for TimerManager
impl Send for TimerManager
impl Sync for TimerManager
Auto Trait Implementations§
impl Freeze for TimerManager
impl RefUnwindSafe for TimerManager
impl Unpin for TimerManager
impl UnwindSafe for TimerManager
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more