| Crates.io | scope-cell |
| lib.rs | scope-cell |
| version | 0.1.2 |
| created_at | 2024-10-22 20:48:14.799879+00 |
| updated_at | 2024-10-23 01:56:03.296562+00 |
| description | A crate for temporary, scope-bound, and thread-safe mutation of data in Rust. |
| homepage | https://github.com/mavin2009/ghost-cell |
| repository | https://github.com/mavin2009/ghost-cell |
| max_upload_size | |
| id | 1419225 |
| size | 14,059 |
Scope-cell provides a mechanism for temporary, scope-bound mutability of immutable data in Rust. It allows for non-permanent changes within a defined scope while ensuring the original data remains unaffected. This is useful in scenarios where you need temporary mutation without ownership or long-term modification of the underlying data.
Key features include:
Temporary mutation: Enables mutation of data within a scope without requiring ownership.
Automatic reversion: All changes are discarded when the scope ends, reverting to the original state.
Low-overhead mutability: Only clone when necessary to ensure efficient memory use.
Rust safety guarantees: Operates within Rust’s strict borrowing and ownership rules to ensure thread safety and data integrity.
Compile-time safety: No runtime checks are required; Rust's type system ensures safe use.
Scope-cell is useful in scenarios where you need to temporarily mutate immutable data without affecting the original state:
use Scope_cell::ScopeCell;
fn main() {
let immutable_data = vec![1, 2, 3];
{
let mut scope = ScopeCell::new(&immutable_data);
scope.get_mut().push(4); // Temporarily mutate the data
assert_eq!(scope.get().len(), 4); // Verify the change within the scope
} // Changes revert here
assert_eq!(immutable_data.len(), 3); // The original data remains unchanged
}
## How it Works
ScopeCell operates by storing a reference to the original data and optionally cloning it into a temporary mutable version. When the ScopeCell is dropped, any changes made are discarded, and the original data remains unaffected.
Key methods:
* **get()** - Borrow the data (either original or modified).
* **get_mut()** - Mutably borrow the data, cloning the original if necessary.
* **revert()** - Explicitly discard any changes, restoring the original data.
## Installation
To use Scope-cell add the following to your Cargo.toml
```toml
[dependencies]
scope-cell = "0.1.2"
## License
This project is licensed under the MIT License.