Skip to content

Commit

Permalink
Add comment explaining Mutex<unsync::OnceCell>.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Oct 6, 2022
1 parent 32dada4 commit 7d9afdb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/imp_cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
use atomic_polyfill::{AtomicBool, Ordering};
use critical_section::{CriticalSection, Mutex};

use crate::unsync::OnceCell as UnsyncOnceCell;
use crate::unsync;

pub(crate) struct OnceCell<T> {
initialized: AtomicBool,
value: Mutex<UnsyncOnceCell<T>>,
// Use `unsync::OnceCell` internally since `Mutex` does not provide
// interior mutability and to be able to re-use `get_or_try_init`.
value: Mutex<unsync::OnceCell<T>>,
}

// Why do we need `T: Send`?
Expand All @@ -23,13 +25,13 @@ impl<T: UnwindSafe> UnwindSafe for OnceCell<T> {}

impl<T> OnceCell<T> {
pub(crate) const fn new() -> OnceCell<T> {
OnceCell { initialized: AtomicBool::new(false), value: Mutex::new(UnsyncOnceCell::new()) }
OnceCell { initialized: AtomicBool::new(false), value: Mutex::new(unsync::OnceCell::new()) }
}

pub(crate) const fn with_value(value: T) -> OnceCell<T> {
OnceCell {
initialized: AtomicBool::new(true),
value: Mutex::new(UnsyncOnceCell::with_value(value)),
value: Mutex::new(unsync::OnceCell::with_value(value)),
}
}

Expand Down

0 comments on commit 7d9afdb

Please sign in to comment.