pub struct SimpleTimer { /* private fields */ }Expand description
Simple One-Shot Timer
A simple timer for one-shot timing operations. Unlike Ton, this timer
does NOT follow the IEC 61131-3 standard and is designed for imperative
“start then check” patterns rather than cyclic function block calls.
Note: For standard PLC timer patterns (delay while condition is true),
use Ton instead. SimpleTimer is intended for one-shot use cases
like “do X, wait, then do Y”.
§Safety
This timer uses Instant (monotonic clock) internally, making it immune
to system clock adjustments (NTP sync, manual changes, DST). This is
critical for reliable timing in industrial control applications.
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_millis(100));
// Timer hasn't started yet
assert_eq!(timer.is_done(), false);
assert_eq!(timer.elapsed(), Duration::ZERO);
// Start the timer
timer.start();
// Check if done (will be false initially)
assert_eq!(timer.is_done(), false);
// After waiting...
std::thread::sleep(Duration::from_millis(110));
assert_eq!(timer.is_done(), true);
// Reset for reuse
timer.reset();
assert_eq!(timer.is_done(), false);§Use Cases
- One-shot delays (“wait 500ms then continue”)
- Tracking time since an event occurred
- Non-cyclic async contexts
- Simple timeout checks
§Comparison with Ton
| Aspect | Ton | SimpleTimer |
|---|---|---|
| IEC 61131-3 | Yes | No |
| Pattern | Cyclic call() | Imperative start()/is_done() |
| Auto-reset on disable | Yes | No (explicit reset()) |
| Best for | PLC-style control | One-shot operations |
Implementations§
Source§impl SimpleTimer
impl SimpleTimer
Sourcepub fn new(preset: Duration) -> Self
pub fn new(preset: Duration) -> Self
Creates a new simple timer with the specified preset duration.
The timer starts in the stopped state and must be explicitly started
with start().
§Arguments
preset- The duration after whichis_done()returnstrue
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let timer = SimpleTimer::new(Duration::from_secs(5));
assert_eq!(timer.is_done(), false);Sourcepub fn start(&mut self)
pub fn start(&mut self)
Starts (or restarts) the timer.
Records the current time as the start time. If the timer was already running, this restarts it from zero.
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_millis(100));
timer.start();
// Timer is now counting...Sourcepub fn is_done(&self) -> bool
pub fn is_done(&self) -> bool
Checks if the preset time has elapsed since the timer was started.
Returns false if the timer hasn’t been started yet.
§Returns
true if the timer was started and the preset duration has elapsed,
false otherwise.
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_millis(50));
// Not started yet
assert_eq!(timer.is_done(), false);
timer.start();
std::thread::sleep(Duration::from_millis(60));
assert_eq!(timer.is_done(), true);Sourcepub fn elapsed(&self) -> Duration
pub fn elapsed(&self) -> Duration
Returns the elapsed time since the timer was started.
Returns Duration::ZERO if the timer hasn’t been started yet.
§Returns
The elapsed time since start() was called, or
Duration::ZERO if not started.
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_secs(10));
// Not started
assert_eq!(timer.elapsed(), Duration::ZERO);
timer.start();
std::thread::sleep(Duration::from_millis(50));
assert!(timer.elapsed() >= Duration::from_millis(50));Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the timer to its initial (stopped) state.
After calling this, is_done() will return false
and elapsed() will return zero until
start() is called again.
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_millis(50));
timer.start();
std::thread::sleep(Duration::from_millis(60));
assert_eq!(timer.is_done(), true);
timer.reset();
assert_eq!(timer.is_done(), false);
assert_eq!(timer.elapsed(), Duration::ZERO);Sourcepub fn preset(&self) -> Duration
pub fn preset(&self) -> Duration
Returns the preset duration for this timer.
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let timer = SimpleTimer::new(Duration::from_secs(5));
assert_eq!(timer.preset(), Duration::from_secs(5));Sourcepub fn set_preset(&mut self, preset: Duration)
pub fn set_preset(&mut self, preset: Duration)
Sets a new preset duration.
This does not reset or restart the timer. If the timer is running,
the new preset takes effect immediately for is_done()
checks.
§Arguments
preset- The new duration after whichis_done()returnstrue
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_secs(5));
timer.set_preset(Duration::from_secs(10));
assert_eq!(timer.preset(), Duration::from_secs(10));Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Returns whether the timer is currently running (has been started but not reset).
§Example
use autocore_std::SimpleTimer;
use std::time::Duration;
let mut timer = SimpleTimer::new(Duration::from_secs(5));
assert_eq!(timer.is_running(), false);
timer.start();
assert_eq!(timer.is_running(), true);
timer.reset();
assert_eq!(timer.is_running(), false);Trait Implementations§
Source§impl Clone for SimpleTimer
impl Clone for SimpleTimer
Source§fn clone(&self) -> SimpleTimer
fn clone(&self) -> SimpleTimer
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more