| Crates.io | atomic-lend-cell |
| lib.rs | atomic-lend-cell |
| version | 0.1.0 |
| created_at | 2025-04-26 02:08:27.101371+00 |
| updated_at | 2025-04-26 02:08:27.101371+00 |
| description | A Rust library for lending immutable references across threads with explicit owner-borrower semantics, offering both atomic reference counting and lightweight flag-based implementations. |
| homepage | |
| repository | https://github.com/su-z/atomic-lend-cell.git |
| max_upload_size | |
| id | 1649797 |
| size | 22,449 |
A thread-safe Rust container that allows lending references to data across thread boundaries with clear ownership semantics.
Rust's ownership model excels at preventing memory safety issues, but sometimes the borrow checker is too restrictive for certain patterns, especially across thread boundaries. When the compiler can't statically verify that borrowing relationships are safe, developers often turn to Arc<T> as a solution, but this comes with downsides:
Arc<T> creates shared ownership where any handle can keep data aliveAtomicLendCell is designed for scenarios where:
T: Sync, not T: Send + SyncAdd the dependency to your Cargo.toml:
[dependencies]
atomic-lend-cell = "0.1.0"
use atomic_lend_cell::AtomicLendCell;
use std::thread;
fn main() {
// Create a cell with some data
let cell = AtomicLendCell::new(vec![1, 2, 3, 4, 5]);
// Borrow the data
let borrow = cell.borrow();
// Use the borrow in another thread
let handle = thread::spawn(move || {
// Access the data through the borrow
println!("Data in thread: {:?}", *borrow);
});
// Meanwhile, the original thread can still access the data
println!("Original data: {:?}", *cell);
// Wait for the thread to complete
handle.join().unwrap();
}
This library offers two different implementations with different performance characteristics:
Arc)[dependencies]
atomic-lend-cell = { version = "0.1.0", default-features = false, features = ["ref-counting"] }
This implementation:
[dependencies]
atomic-lend-cell = "0.1.0" # Uses flag-based by default
This implementation:
AtomicLendCell enforces safety by ensuring:
In debug builds, violations of the borrowing contract will cause panics to catch issues early. In release builds, some checks may be optimized away for performance.
⚠️ Important Safety Warning
Both implementations will panic if the AtomicLendCell is dropped while active borrowers exist, however:
Reference counting implementation: Will reliably panic as soon as the owner is dropped with active borrows, providing strong safety guarantees.
Flag-based implementation: The panic is based on checking an atomic flag during specific operations. In rare cases with concurrent access across threads, a segmentation fault might occur before the panic is triggered, particularly in release builds or high-concurrency scenarios.
If your application requires absolute memory safety guarantees, consider:
ref-counting feature)Arc<T> when appropriateThe flag-based implementation (default) prioritizes performance at the cost of some safety guarantees, so use it when you're confident about your borrowing patterns and ownership lifecycle.
AtomicLendCell is ideal for:
Arc introduces too much overheadConsider alternatives when:
RwLock or Mutex)Arc)This project is licensed under the MIT License - see the LICENSE file for details.