| Crates.io | lendpool |
| lib.rs | lendpool |
| version | 0.1.0 |
| created_at | 2025-01-03 20:45:06.853433+00 |
| updated_at | 2025-01-03 20:45:06.853433+00 |
| description | A simple lock-free library for allowing safe and concurrent access to a group of objects |
| homepage | https://github.com/snowfoxsh/lendpool |
| repository | https://github.com/snowfoxsh/lendpool |
| max_upload_size | |
| id | 1502855 |
| size | 30,378 |
LendPool is a simple lock-free library for allowing safe and concurrent access to a group of objects.
It achieves this with a Loan<T> guard.
use lendpool::LendPool;
fn main() {
// create a new pool
let pool = LendPool::new();
// add items to the pool
pool.add("Resource 1".to_string());
pool.add("Resource 2".to_string());
// attempt to loan an item (non-blocking)
if let Some(loan) = pool.loan() {
println!("Borrowed item: {}", *loan);
// you can transform the borrowed item
loan.with_mut(|val| val.push_str(" - Modified"));
// access the modified value
println!("Modified item: {}", *loan);
}; // loan is dropped here, returning the item to the pool
// loan another item
if let Some(mut loan) = pool.loan() {
// permanently take the item from the pool
let item = loan.take();
println!("Took item: {}", item);
};
// check the pool's status
println!("Available items: {}", pool.available());
println!("Items on loan: {}", pool.on_loan());
println!("Total items: {}", pool.total());
}
sync: allows blocking until a resource is available
async: allows waiting on the pool until a resource is available
LendPool is based on crossbeam_queue::SegQueue.
The async feature uses tokio::sync::Notify to handle waiting.