Execute a function after a mutable duration.
```rust use std::time::Duration; use dyntimeout::stdthread::DynTimeout;
const TWENTY: Duration = Duration::from_millis(20);
let dyntimeout = DynTimeout::new(TWENTY, || { println!("after forty milliseconds"); }); dyntimeout.add(TWENTY).unwrap(); // .sub... // .cancel ```
This library was initially implemented to be used as a raft like election timeout.
This crate include a std with threads and a tokio implementation, usefull if you're already using this async library.
```rust use tokio::runtime::Runtime; use dyntimeout::tokioimpl::DynTimeout; use std::time::Duration; const TWENTY: Duration = Duration::from_millis(20);
let mut rt = Runtime::new().unwrap(); rt.spawn(async { let dyntimeout = DynTimeout::new(TWENTY, || { println!("after forty milliseconds"); }); dyntimeout.add(TWENTY).await.unwrap(); }); ```
Here is the bench with 40 milliseconds to wait with the standard implementation, under the nanoseconds the time precision decrease. (Using tokio decrease also the precision)
bash
test test::simple_bench ... bench: 40,641,475 ns/iter (+/- 68,064)
All development contribution, please, has to pass the currents unit tests and it's a must to include a new test!
This lirary is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.