2021-11-22 16:28:30 +00:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub struct SingleShotEventual<T>
|
|
|
|
where
|
2022-05-24 21:13:52 +00:00
|
|
|
T: Unpin,
|
2021-11-22 16:28:30 +00:00
|
|
|
{
|
2022-05-24 21:13:52 +00:00
|
|
|
eventual: EventualValue<T>,
|
|
|
|
drop_value: Option<T>,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Drop for SingleShotEventual<T>
|
|
|
|
where
|
2022-05-24 21:13:52 +00:00
|
|
|
T: Unpin,
|
2021-11-22 16:28:30 +00:00
|
|
|
{
|
|
|
|
fn drop(&mut self) {
|
2022-05-24 21:13:52 +00:00
|
|
|
if let Some(drop_value) = self.drop_value.take() {
|
|
|
|
self.eventual.resolve(drop_value);
|
|
|
|
}
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> SingleShotEventual<T>
|
|
|
|
where
|
2022-05-24 21:13:52 +00:00
|
|
|
T: Unpin,
|
2021-11-22 16:28:30 +00:00
|
|
|
{
|
2022-05-24 21:13:52 +00:00
|
|
|
pub fn new(drop_value: Option<T>) -> Self {
|
2021-11-22 16:28:30 +00:00
|
|
|
Self {
|
2022-05-24 21:13:52 +00:00
|
|
|
eventual: EventualValue::new(),
|
2021-11-27 17:44:21 +00:00
|
|
|
drop_value,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can only call this once, it consumes the eventual
|
2022-05-24 21:13:52 +00:00
|
|
|
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 16:28:30 +00:00
|
|
|
self.eventual.resolve(value)
|
|
|
|
}
|
|
|
|
|
2022-05-24 21:13:52 +00:00
|
|
|
pub fn instance(&self) -> EventualValueFuture<T> {
|
2021-11-22 16:28:30 +00:00
|
|
|
self.eventual.instance()
|
|
|
|
}
|
|
|
|
}
|