pub struct DynTimeout { /* private fields */ }
Expand description
Dynamic timeout, standard implementation with std::thread. Automaticcaly join on drop.
§Example
use std::time::Duration;
use dyn_timeout::std_thread::DynTimeout;
const TWENTY: Duration = Duration::from_millis(20);
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).unwrap();
Implementations§
Source§impl DynTimeout
impl DynTimeout
Sourcepub fn new(dur: Duration, callback: fn()) -> Self
pub fn new(dur: Duration, callback: fn()) -> Self
Create a new dynamic timeout in a new thread. Execute the callback function in the separated thread after a given duration. The created thread join automatically on drop timeout without dismiss the callback execution.
§Example
use std::time::Duration;
use dyn_timeout::std_thread::DynTimeout;
const TWENTY: Duration = Duration::from_millis(20);
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).unwrap();
Sourcepub fn add(&self, dur: Duration) -> Result<()>
pub fn add(&self, dur: Duration) -> Result<()>
Increase the delay before the timeout.
§Return
Return a result with an error if the timeout already appened or it failed to increase the delay for any other reason. Otherwise it return an empty success.
§Example
use std::time::Duration;
use dyn_timeout::std_thread::DynTimeout;
const TWENTY: Duration = Duration::from_millis(20);
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).unwrap();
Sourcepub fn sub(&self, dur: Duration) -> Result<()>
pub fn sub(&self, dur: Duration) -> Result<()>
Try to decrease the delay before the timeout. (bad precision, work in progress)
§Return
Return a result with an error if the timeout already appened or it failed to decrease the delay for any other reason. Otherwise it return an empty success.
§Example
use std::time::Duration;
use dyn_timeout::std_thread::DynTimeout;
const TWENTY: Duration = Duration::from_millis(20);
const TEN: Duration = Duration::from_millis(10);
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after some milliseconds");
});
dyn_timeout.add(TEN).unwrap();
dyn_timeout.add(TWENTY).unwrap();
dyn_timeout.sub(TEN).unwrap();
Sourcepub fn cancel(&mut self) -> Result<()>
pub fn cancel(&mut self) -> Result<()>
Dismiss the timeout callback and cancel all delays added. Stop immediatelly all waiting process and join the created thread.
§Return
Return a result with an error if the timeout if the program failed to clear the delays. Otherwise it return an empty success.
§Example
use std::time::Duration;
use dyn_timeout::std_thread::DynTimeout;
const TWENTY: Duration = Duration::from_millis(20);
const TEN: Duration = Duration::from_millis(10);
let mut dyn_timeout = DynTimeout::new(TWENTY, || {
println!("never append");
});
dyn_timeout.add(TEN).unwrap();
// cancel the last ten milliseconds and dismiss the callback
dyn_timeout.cancel().unwrap();