Simplify OnceMap::wait_blocking (#4704)

## Summary

De-duplicate by calling directly into the async version.
This commit is contained in:
Ibraheem Ahmed 2024-07-01 15:17:37 -04:00 committed by GitHub
parent f3d1e52e65
commit ea031461c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -77,30 +77,7 @@ impl<K: Eq + Hash, V: Clone, H: BuildHasher + Clone> OnceMap<K, V, H> {
/// ///
/// Will hang if [`OnceMap::done`] isn't called for this key. /// Will hang if [`OnceMap::done`] isn't called for this key.
pub fn wait_blocking(&self, key: &K) -> Option<V> { pub fn wait_blocking(&self, key: &K) -> Option<V> {
let notify = { futures::executor::block_on(self.wait(key))
let entry = self.items.get(key)?;
match entry.value() {
Value::Filled(value) => return Some(value.clone()),
Value::Waiting(notify) => notify.clone(),
}
};
// Register the waiter for calls to `notify_waiters`.
let notification = pin!(notify.notified());
// Make sure the value wasn't inserted in-between us checking the map and registering the waiter.
if let Value::Filled(value) = self.items.get(key).expect("map is append-only").value() {
return Some(value.clone());
};
// Wait until the value is inserted.
futures::executor::block_on(notification);
let entry = self.items.get(key).expect("map is append-only");
match entry.value() {
Value::Filled(value) => Some(value.clone()),
Value::Waiting(_) => unreachable!("notify was called"),
}
} }
/// Return the result of a previous job, if any. /// Return the result of a previous job, if any.