lendpool

Crates.iolendpool
lib.rslendpool
version0.1.0
created_at2025-01-03 20:45:06.853433+00
updated_at2025-01-03 20:45:06.853433+00
descriptionA simple lock-free library for allowing safe and concurrent access to a group of objects
homepagehttps://github.com/snowfoxsh/lendpool
repositoryhttps://github.com/snowfoxsh/lendpool
max_upload_size
id1502855
size30,378
Patrick Unick (snowfoxsh)

documentation

https://docs.rs/lendpool

README

LendPool

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.

Example Usage

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());
}

Feature Flags

  • sync: allows blocking until a resource is available

  • async: allows waiting on the pool until a resource is available

Dependencies

LendPool is based on crossbeam_queue::SegQueue. The async feature uses tokio::sync::Notify to handle waiting.

Commit count: 16

cargo fmt