Crate ephemeropt
source ·Expand description
An ephemeral Option
for Rust. When created, this EphemeralOption
takes an expiry time and a value,
and the EphemeralOption
will revert to None
after the time runs out.
Example
use ephemeropt::EphemeralOption;
let mut num_opt = EphemeralOption::new(0, std::time::Duration::from_secs(1));
// Will only go up to 10, because every other call will be cached
for _ in 0..=20 {
match num_opt.get() {
Some(&num) => println!("{num}"),
None => {
let prev_num = num_opt.get_expired().unwrap();
let num = num_opt.insert(prev_num + 1);
println!("{num}");
}
}
std::thread::sleep(std::time::Duration::from_millis(500));
}
Structs
- An
Option
that automatically reverts toNone
after a certain amount of time