From 7d9afdb9d794b549b87a6d7c6611e3a28ec1588a Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 17 Sep 2022 05:32:52 +0200 Subject: [PATCH] Add comment explaining `Mutex`. --- src/imp_cs.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/imp_cs.rs b/src/imp_cs.rs index 985ea6f..2548f58 100644 --- a/src/imp_cs.rs +++ b/src/imp_cs.rs @@ -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 { initialized: AtomicBool, - value: Mutex>, + // Use `unsync::OnceCell` internally since `Mutex` does not provide + // interior mutability and to be able to re-use `get_or_try_init`. + value: Mutex>, } // Why do we need `T: Send`? @@ -23,13 +25,13 @@ impl UnwindSafe for OnceCell {} impl OnceCell { pub(crate) const fn new() -> OnceCell { - 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 { OnceCell { initialized: AtomicBool::new(true), - value: Mutex::new(UnsyncOnceCell::with_value(value)), + value: Mutex::new(unsync::OnceCell::with_value(value)), } }