|
20 | 20 | use crate::owning_ref::{Erased, OwningRef}; |
21 | 21 | use std::collections::HashMap; |
22 | 22 | use std::hash::{BuildHasher, Hash}; |
23 | | -use std::marker::PhantomData; |
24 | 23 | use std::ops::{Deref, DerefMut}; |
25 | 24 |
|
26 | 25 | pub use std::sync::atomic::Ordering; |
@@ -230,6 +229,8 @@ cfg_if! { |
230 | 229 | pub use std::cell::RefMut as LockGuard; |
231 | 230 | pub use std::cell::RefMut as MappedLockGuard; |
232 | 231 |
|
| 232 | + pub use once_cell::unsync::OnceCell; |
| 233 | + |
233 | 234 | use std::cell::RefCell as InnerRwLock; |
234 | 235 | use std::cell::RefCell as InnerLock; |
235 | 236 |
|
@@ -313,6 +314,8 @@ cfg_if! { |
313 | 314 | pub use parking_lot::MutexGuard as LockGuard; |
314 | 315 | pub use parking_lot::MappedMutexGuard as MappedLockGuard; |
315 | 316 |
|
| 317 | + pub use once_cell::sync::OnceCell; |
| 318 | + |
316 | 319 | pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64}; |
317 | 320 |
|
318 | 321 | pub use crossbeam_utils::atomic::AtomicCell; |
@@ -432,134 +435,6 @@ impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S> |
432 | 435 | } |
433 | 436 | } |
434 | 437 |
|
435 | | -/// A type whose inner value can be written once and then will stay read-only |
436 | | -// This contains a PhantomData<T> since this type conceptually owns a T outside the Mutex once |
437 | | -// initialized. This ensures that Once<T> is Sync only if T is. If we did not have PhantomData<T> |
438 | | -// we could send a &Once<Cell<bool>> to multiple threads and call `get` on it to get access |
439 | | -// to &Cell<bool> on those threads. |
440 | | -pub struct Once<T>(Lock<Option<T>>, PhantomData<T>); |
441 | | - |
442 | | -impl<T> Once<T> { |
443 | | - /// Creates an Once value which is uninitialized |
444 | | - #[inline(always)] |
445 | | - pub fn new() -> Self { |
446 | | - Once(Lock::new(None), PhantomData) |
447 | | - } |
448 | | - |
449 | | - /// Consumes the value and returns Some(T) if it was initialized |
450 | | - #[inline(always)] |
451 | | - pub fn into_inner(self) -> Option<T> { |
452 | | - self.0.into_inner() |
453 | | - } |
454 | | - |
455 | | - /// Tries to initialize the inner value to `value`. |
456 | | - /// Returns `None` if the inner value was uninitialized and `value` was consumed setting it |
457 | | - /// otherwise if the inner value was already set it returns `value` back to the caller |
458 | | - #[inline] |
459 | | - pub fn try_set(&self, value: T) -> Option<T> { |
460 | | - let mut lock = self.0.lock(); |
461 | | - if lock.is_some() { |
462 | | - return Some(value); |
463 | | - } |
464 | | - *lock = Some(value); |
465 | | - None |
466 | | - } |
467 | | - |
468 | | - /// Tries to initialize the inner value to `value`. |
469 | | - /// Returns `None` if the inner value was uninitialized and `value` was consumed setting it |
470 | | - /// otherwise if the inner value was already set it asserts that `value` is equal to the inner |
471 | | - /// value and then returns `value` back to the caller |
472 | | - #[inline] |
473 | | - pub fn try_set_same(&self, value: T) -> Option<T> |
474 | | - where |
475 | | - T: Eq, |
476 | | - { |
477 | | - let mut lock = self.0.lock(); |
478 | | - if let Some(ref inner) = *lock { |
479 | | - assert!(*inner == value); |
480 | | - return Some(value); |
481 | | - } |
482 | | - *lock = Some(value); |
483 | | - None |
484 | | - } |
485 | | - |
486 | | - /// Tries to initialize the inner value to `value` and panics if it was already initialized |
487 | | - #[inline] |
488 | | - pub fn set(&self, value: T) { |
489 | | - assert!(self.try_set(value).is_none()); |
490 | | - } |
491 | | - |
492 | | - /// Initializes the inner value if it wasn't already done by calling the provided closure. It |
493 | | - /// ensures that no-one else can access the value in the mean time by holding a lock for the |
494 | | - /// duration of the closure. |
495 | | - /// A reference to the inner value is returned. |
496 | | - #[inline] |
497 | | - pub fn init_locking<F: FnOnce() -> T>(&self, f: F) -> &T { |
498 | | - { |
499 | | - let mut lock = self.0.lock(); |
500 | | - if lock.is_none() { |
501 | | - *lock = Some(f()); |
502 | | - } |
503 | | - } |
504 | | - |
505 | | - self.borrow() |
506 | | - } |
507 | | - |
508 | | - /// Tries to initialize the inner value by calling the closure without ensuring that no-one |
509 | | - /// else can access it. This mean when this is called from multiple threads, multiple |
510 | | - /// closures may concurrently be computing a value which the inner value should take. |
511 | | - /// Only one of these closures are used to actually initialize the value. |
512 | | - /// If some other closure already set the value, |
513 | | - /// we return the value our closure computed wrapped in a `Option`. |
514 | | - /// If our closure set the value, `None` is returned. |
515 | | - /// If the value is already initialized, the closure is not called and `None` is returned. |
516 | | - #[inline] |
517 | | - pub fn init_nonlocking<F: FnOnce() -> T>(&self, f: F) -> Option<T> { |
518 | | - if self.0.lock().is_some() { None } else { self.try_set(f()) } |
519 | | - } |
520 | | - |
521 | | - /// Tries to initialize the inner value by calling the closure without ensuring that no-one |
522 | | - /// else can access it. This mean when this is called from multiple threads, multiple |
523 | | - /// closures may concurrently be computing a value which the inner value should take. |
524 | | - /// Only one of these closures are used to actually initialize the value. |
525 | | - /// If some other closure already set the value, we assert that it our closure computed |
526 | | - /// a value equal to the value already set and then |
527 | | - /// we return the value our closure computed wrapped in a `Option`. |
528 | | - /// If our closure set the value, `None` is returned. |
529 | | - /// If the value is already initialized, the closure is not called and `None` is returned. |
530 | | - #[inline] |
531 | | - pub fn init_nonlocking_same<F: FnOnce() -> T>(&self, f: F) -> Option<T> |
532 | | - where |
533 | | - T: Eq, |
534 | | - { |
535 | | - if self.0.lock().is_some() { None } else { self.try_set_same(f()) } |
536 | | - } |
537 | | - |
538 | | - /// Tries to get a reference to the inner value, returns `None` if it is not yet initialized |
539 | | - #[inline(always)] |
540 | | - pub fn try_get(&self) -> Option<&T> { |
541 | | - let lock = &*self.0.lock(); |
542 | | - if let Some(ref inner) = *lock { |
543 | | - // This is safe since we won't mutate the inner value |
544 | | - unsafe { Some(&*(inner as *const T)) } |
545 | | - } else { |
546 | | - None |
547 | | - } |
548 | | - } |
549 | | - |
550 | | - /// Gets reference to the inner value, panics if it is not yet initialized |
551 | | - #[inline(always)] |
552 | | - pub fn get(&self) -> &T { |
553 | | - self.try_get().expect("value was not set") |
554 | | - } |
555 | | - |
556 | | - /// Gets reference to the inner value, panics if it is not yet initialized |
557 | | - #[inline(always)] |
558 | | - pub fn borrow(&self) -> &T { |
559 | | - self.get() |
560 | | - } |
561 | | -} |
562 | | - |
563 | 438 | #[derive(Debug)] |
564 | 439 | pub struct Lock<T>(InnerLock<T>); |
565 | 440 |
|
|
0 commit comments