Struct TimerManager

Source
pub struct TimerManager { /* private fields */ }
Expand description

A manager for controlling multiple timers.

Implementations§

Source§

impl TimerManager

Source

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}
Source

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}
Source

pub fn stop_all(&self)

Stops all timers.

Source

pub fn list_timers(&self) -> Vec<u64>

Lists all active timers.

Source

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

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Send for TimerManager

Source§

impl Sync for TimerManager

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.