I am working on a Rust project where I store a BeaconInfoEventContent
struct in a BTreeMap
. Here is my current implementation:
pub struct BeaconInfoEventContent {
pub live: bool,
pub ts: MilliSecondsSinceUnixEpoch,
pub timeout: Duration,
}
impl BeaconInfoEventContent {
pub fn new(
timeout: Duration,
live: bool,
ts: Option<MilliSecondsSinceUnixEpoch>,
) -> Self {
Self {
live,
ts: ts.unwrap_or_else(MilliSecondsSinceUnixEpoch::now),
timeout,
}
}
pub fn is_live(&self) -> bool {
self.live
&& self
.ts
.to_system_time()
.and_then(|t| t.checked_add(self.timeout))
.is_some_and(|t| t > SystemTime::now())
}
}
ExampleStruct {
...
pub(crate) beacons: BTreeMap<OwnedUserId, BeaconInfoEventContent>,
....
}
I need to automatically update the BeaconInfoEventContent
‘s live
field when the duration expires. Is there a proper approach to this besides constant polling at a specific interval?