#cell #once-cell #once #lazy-evaluation

unmaintained double-checked-cell

A thread-safe lazily initialized cell using double-checked locking

8 stable releases

Uses old Rust 2015

2.1.0 Dec 12, 2020
2.0.3 Jun 24, 2020
2.0.2 Jul 27, 2019
2.0.1 Dec 1, 2018
1.0.0 Nov 2, 2017

#1234 in Concurrency

Download history 72/week @ 2024-07-22 140/week @ 2024-07-29 201/week @ 2024-08-05 77/week @ 2024-08-12 226/week @ 2024-08-19 23/week @ 2024-08-26 27/week @ 2024-09-02 33/week @ 2024-09-09 12/week @ 2024-09-16 61/week @ 2024-09-23 140/week @ 2024-09-30 53/week @ 2024-10-07 90/week @ 2024-10-14 108/week @ 2024-10-21 86/week @ 2024-10-28 133/week @ 2024-11-04

425 downloads per month
Used in 4 crates (3 directly)

MIT/Apache

19KB
153 lines

double-checked-cell

A thread-safe lazily initialized cell using double-checked locking.

Build Status crates.io docs.rs No maintenance intended

Deprecated

You should probably use once_cell instead of this crate. It provides a superset of this crate's functionality, with a nicely consistent API. Its functionality is also being considered for inclusion in the standard library.

Introduction

Provides a memory location that can be safely shared between threads and initialized at most once. Once the cell is initialized it becomes immutable.

If you do not need to change the value after initialization DoubleCheckedCell<T> is more efficient than a Mutex<Option<T>>.

extern crate double_checked_cell;

use double_checked_cell::DoubleCheckedCell;

fn main() {
    let cell = DoubleCheckedCell::new();

    // The cell starts uninitialized.
    assert_eq!(cell.get(), None);

    // Perform potentially expensive initialization.
    let value = cell.get_or_init(|| 21 + 21);
    assert_eq!(*value, 42);
    assert_eq!(cell.get(), Some(&42));

    // The cell is already initialized.
    let value = cell.get_or_init(|| unreachable!());
    assert_eq!(*value, 42);
    assert_eq!(cell.get(), Some(&42));
}

These crates are similar but distinct by design:

  • lazy-init – Based on a LazyTransform<T, U> which can lazily consume T to produce an U. Therefore cannot support fallible initialization.
  • lazycellAtomicLazyCell does not support lazy initialization (unlike its non-thread-safe counterpart LazyCell using LazyCell::borrow_with()).
  • mitochondria – Not Sync.
  • lazy_static - With the optional (currently nightly only) const_fn feature, DoubleCheckedCell::new() can also be used in static/const context. However lazy_static! is more convenient when there is only a single way to initialize the cell.

Documentation

Read the documentation

Changelog

  • 2.1.0
    • Fix UnwindSafe and RefUnwindSafe bounds. This is technically a breaking change, but published under the same major version because it is a bugfix.
  • 2.0.3
    • Update to parking_lot 0.11.
  • 2.0.2
    • Update to parking_lot 0.9.
  • 2.0.1
    • Update to parking_lot 0.7.
  • 2.0.0
    • Changed unwinding behavior: DoubleCheckedCell no longer implements poisoning.
    • New optional cargo features: parking_lot_mutex, const_fn.
  • 1.1.0
    • Fix unsoundness: DoubleCheckedCell<T> where T: !Send cannot be Sync.
  • 1.0.1
    • Ignore unused_unsafe warning due to UnsafeCell::into_inner() no longer beeing unsafe.
  • 1.0.0
    • Initial release.

License

double-checked-cell is licensed under the Apache 2.0 and MIT license, at your option.

Dependencies

~10–315KB