Skip to content

Commit

Permalink
refactor: use take instead of hopeful Option::map
Browse files Browse the repository at this point in the history
intention is clearer, and we avoid the unnecessary reinit of a new
semaphore.
  • Loading branch information
koivunej committed Apr 16, 2024
1 parent e69a168 commit bf75eb8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions libs/utils/src/sync/heavier_once_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,17 @@ impl<'a, T> Guard<'a, T> {

impl<T> Inner<T> {
pub fn take_and_deinit(&mut self) -> Option<(T, InitPermit)> {
let Some(value) = self.value.take() else {
return None;
};

let mut swapped = Inner::default();
let sem = swapped.init_semaphore.clone();
// acquire and forget right away, moving the control over to InitPermit
sem.try_acquire().expect("we just created this").forget();
std::mem::swap(self, &mut swapped);
let permit = InitPermit(sem);
swapped.value.map(|v| (v, permit))
std::mem::swap(self, &mut swapped);
Some((value, permit))
}
}

Expand Down

0 comments on commit bf75eb8

Please sign in to comment.