veilid/veilid-tools/src/single_shot_eventual.rs

45 lines
974 B
Rust
Raw Normal View History

2021-11-22 11:28:30 -05:00
use super::*;
pub struct SingleShotEventual<T>
where
T: Unpin,
2021-11-22 11:28:30 -05:00
{
eventual: EventualValue<T>,
drop_value: Option<T>,
2021-11-22 11:28:30 -05:00
}
impl<T> Drop for SingleShotEventual<T>
where
T: Unpin,
2021-11-22 11:28:30 -05:00
{
fn drop(&mut self) {
if let Some(drop_value) = self.drop_value.take() {
self.eventual.resolve(drop_value);
}
2021-11-22 11:28:30 -05:00
}
}
impl<T> SingleShotEventual<T>
where
T: Unpin,
2021-11-22 11:28:30 -05:00
{
pub fn new(drop_value: Option<T>) -> Self {
2021-11-22 11:28:30 -05:00
Self {
eventual: EventualValue::new(),
2021-11-27 12:44:21 -05:00
drop_value,
2021-11-22 11:28:30 -05:00
}
}
// Can only call this once, it consumes the eventual
pub fn resolve(mut self, value: T) -> EventualResolvedFuture<EventualValue<T>> {
// If we resolve, we don't want to resolve again to the drop value
self.drop_value = None;
// Resolve to the specified value
2021-11-22 11:28:30 -05:00
self.eventual.resolve(value)
}
pub fn instance(&self) -> EventualValueFuture<T> {
2021-11-22 11:28:30 -05:00
self.eventual.instance()
}
}