Struct dyn_timeout::tokio_impl::DynTimeout [−][src]
pub struct DynTimeout { /* fields omitted */ }Expand description
Dynamic timeout, async implementation with the tokio library.
Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);
let mut rt = Runtime::new().unwrap();
rt.spawn(async {
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).await.unwrap();
});Implementations
Create a new dynamic timeout in a new thread. Execute the callback function in the separated thread after a given duration.
Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);
let mut rt = Runtime::new().unwrap();
rt.spawn(async {
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after forty milliseconds");
});
dyn_timeout.add(TWENTY).await.unwrap();
});Increase the delay before the timeout.
Return
Return a result with an error if the timeout already appened. Otherwise it return an empty success.
Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);
let mut rt = Runtime::new().unwrap();
rt.spawn(async {
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after some milliseconds");
});
dyn_timeout.add(TWENTY).await.unwrap();
});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. Otherwise it return an empty success.
Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);
const TEN: Duration = Duration::from_millis(10);
let mut rt = Runtime::new().unwrap();
rt.spawn(async {
let dyn_timeout = DynTimeout::new(TWENTY, || {
println!("after some milliseconds");
});
dyn_timeout.add(TEN).await.unwrap();
dyn_timeout.add(TWENTY).await.unwrap();
dyn_timeout.sub(TEN).await.unwrap();
});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 already appened. Otherwise it return an empty success.
Example
use tokio::runtime::Runtime;
use dyn_timeout::tokio_impl::DynTimeout;
use std::time::Duration;
const TWENTY: Duration = Duration::from_millis(20);
const TEN: Duration = Duration::from_millis(10);
let mut rt = Runtime::new().unwrap();
rt.spawn(async {
let mut dyn_timeout = DynTimeout::new(TWENTY, || {
println!("never append");
});
dyn_timeout.cancel().await.unwrap();
});