Crate minipool

Source
Expand description

§minipool

Lightweight, generic tokio-based pool implementation for Rust.

§Usage

use std::ops::{Deref, DerefMut};

use minipool::{Pool, PoolEntry};

const BUF_SIZE: usize = 9000;
struct Buf(Box<[u8; BUF_SIZE]>);

impl PoolEntry for Buf {
  fn is_closed(&self) -> bool {
    false
  }
}

// allows to call methods from `&[u8]` directly on `Buf`
impl Deref for Buf {
  type Target = Box<[u8; BUF_SIZE]>;
  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

// allows to call methods from `&mut [u8]` directly on `Buf`
impl DerefMut for Buf {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

async fn run() {
  let pool = Pool::default();
  pool.push(Buf(Box::new([0u8; BUF_SIZE])));

  {
    let buf = pool.acquire().await;
    // do things with `buf`
  }

  // the buffer got reinserted into the pool
}

§License

Licensed under either of

at your option.

§Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Structs§

Pool
Lightweight, generic tokio-based pool implementation for Rust.
PoolGuard
A wrapper around an occupied entry of a Pool.

Traits§

PoolEntry
Repsesents a entity that can be a member of a Pool.