setup_read_cleanup

A library for managing data through distinct lifecycle phases: Setup -> Read -> Cleanup.
This crate provides "phased cells", a collection of smart pointer types that enforce a specific lifecycle for the data they manage. This is useful for data that is initialized once, read by multiple threads or tasks for a period, and then explicitly destroyed.
Core Concepts
The lifecycle is divided into three phases:
Setup: The initial phase. The data can be mutably accessed for initialization.Read: The operational phase. The data can only be accessed immutably. This phase is optimized for concurrent, lock-free reads.Cleanup: The final phase. The data can be mutably accessed again for deconstruction or resource cleanup.
Panic Handling in Transition Closures
The transition_to_read and transition_to_cleanup methods execute user-provided closures. If a panic occurs within these closures, PhasedCell ensures the following behavior across all its variants:
transition_to_read: If the closure panics, the cell's phase is safely reverted toSetup.transition_to_cleanup: If the closure panics, the cell's phase is safely transitioned toCleanup.
In both cases, any acquired internal mutexes are released before the panic is resumed, preventing deadlocks. The original panic is re-propagated, maintaining Rust's panic-safety guarantees while ensuring internal consistency.
Cell Variants
This crate offers several cell variants to suit different concurrency needs:
PhasedCell: The basic cell. It isSync, allowing it to be shared across threads for reading. However, mutable access viaget_mut_unlockedis not thread-safe and requires the caller to ensure exclusive access.PhasedCellSync: A thread-safe version that uses astd::sync::Mutexto allow for safe concurrent mutable access during theSetupandCleanupphases.PhasedCellAsync: (Requires thetokiofeature) Anasyncversion ofPhasedCellSyncthat uses atokio::sync::Mutex.
Graceful Features
(Requires the graceful feature)
The graceful module provides wrappers that add graceful capabilities to phased cells:
- Graceful Cleanup: It ensures that all ongoing read operations complete before allowing
the cell to fully transition to the
Cleanupphase. - Graceful Read: If a read operation is attempted while the cell is in the
Setupphase and transitioning toRead, the read operation will wait for the transition to complete and for the cell to enter theReadphase.
It provides the following cells:
GracefulPhasedCellGracefulPhasedCellSyncGracefulPhasedCellAsync(Requiresgracefulandtokiofeatures)
Features
tokio: Enables theasynccell variants (PhasedCellAsync,GracefulPhasedCellAsync) which usetokio::sync.graceful: Enables thegracefulmodule, which provides cells with graceful cleanup capabilities.
Examples
Using a static PhasedCellSync to initialize data, read it from multiple threads, and then clean it up.
use ;
use thread;
// Declare a static PhasedCellSync instance
static CELL: = new;
Installation
In Cargo.toml, write this crate as a dependency:
[]
= "0.6.0"
Supported Rust versions
This crate supports Rust 1.74.1 or later.
)
License
Copyright (C) 2025 Takayuki Sato
This program is free software under MIT License. See the file LICENSE in this distribution for more details.