Crate tub

source ·
Expand description

An asynchronous pool for managing reusable values.

Values are retrieved from the pool asynchronously. When the retrieved out value goes out of scope, the value is returned to the pool.

Examples

use tub::Pool;

#[tokio::main]
async fn main() {
   // Create a pool
   let pool: Pool<Box> = (0..10)
       .map(|_| Box { value: 123 })
       .into();
   assert_eq!(pool.remaining_capacity(), 10);

   // Get a value from the pool
   let mut box1 = pool.acquire().await;
   assert_eq!(pool.remaining_capacity(), 9);

   // Use the value
   box1.foo();

   // Modify the value
   *box1 = Box { value: 456 };
   assert_eq!(box1.value, 456);

   // Return the value to the pool
   drop(box1);
   assert_eq!(pool.remaining_capacity(), 10);
}

struct Box {
  value: u32
}

impl Box {
  fn foo(&mut self) { }
}

Structs

  • A handle to a value from the pool
  • A shared resource pool