Struct Timer

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

Timer struct for managing one-time and recurring tasks.

Implementations§

Source§

impl Timer

Source

pub fn new() -> Self

Creates a new timer.

Examples found in repository?
examples/feature_showcase.rs (line 40)
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 async fn start_once<F>( &mut self, delay: Duration, callback: F, ) -> Result<(), TimerError>
where F: TimerCallback + 'static,

Starts a one-time timer.

Examples found in repository?
examples/feature_showcase.rs (line 42)
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 async fn start_recurring<F>( &mut self, interval: Duration, callback: F, expiration_count: Option<usize>, ) -> Result<(), TimerError>
where F: TimerCallback + 'static,

Starts a recurring timer with an optional expiration count.

Examples found in repository?
examples/feature_showcase.rs (line 50)
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 async fn pause(&self) -> Result<(), TimerError>

Pauses a running timer.

Examples found in repository?
examples/feature_showcase.rs (line 62)
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 async fn resume(&self) -> Result<(), TimerError>

Resumes a paused timer.

Examples found in repository?
examples/feature_showcase.rs (line 71)
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 async fn stop(&mut self) -> Result<(), TimerError>

Stops the timer.

Source

pub fn adjust_interval( &mut self, new_interval: Duration, ) -> Result<(), TimerError>

Adjusts the interval of a running timer.

Examples found in repository?
examples/feature_showcase.rs (line 78)
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 async fn get_statistics(&self) -> TimerStatistics

Gets the timer’s statistics.

Examples found in repository?
examples/feature_showcase.rs (line 85)
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 async fn get_state(&self) -> TimerState

Gets the current state of the timer.

Source

pub fn get_expiration_count(&self) -> Option<usize>

Trait Implementations§

Source§

impl Clone for Timer

Source§

fn clone(&self) -> Timer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Send for Timer

Source§

impl Sync for Timer

Auto Trait Implementations§

§

impl Freeze for Timer

§

impl !RefUnwindSafe for Timer

§

impl Unpin for Timer

§

impl !UnwindSafe for Timer

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.