blocking_object_pool 0.1.0

a thread safe, blocking, object pool in rust
Documentation
  • Coverage
  • 0%
    0 out of 6 items documented0 out of 5 items with examples
  • Size
  • Source code size: 6.54 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.77 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • liusong1111

BlockingObjectPool

A thread safe, blocking, object pool in rust.

Summary

  • BlockingObjectPool::with_capacity or BlockingObjectPool::with_objects to initialize a pool
  • pool.get to obtain a object from the pool

If the pool is empty, get will be blocked.

If the object is out of scope, it's been put back to the pool automatically.

That's all!

Examples

extern crate blocking_object_pool;

use blocking_object_pool::BlockingObjectPool;

#[derive(Clone)]
struct Hello {
    a: i32,
    b: i32,
}

fn main() {
    let pool = BlockingObjectPool::with_capacity(2, || Hello { a: 1, b: 2 });

    // get one object
    let m = pool.get();
    assert_eq!(m.a, 1);
    println!("one. m.a={}", m.a);

    // get another object.
    {
        let mut m = pool.get();
        println!("two. m.a={}", m.a);
        m.a = 11;
    } // here the 2nd object goes out of scope, and it's been put back to the pool automatically.
    // you can either call drop(m) manually.

    let m = pool.get();
    assert_eq!(m.a, 11);
    println!("two again, m.a={}", m.a);

    println!("this line will be blocked for ever");
    let _m = pool.get();
}

Inspired by zslayton's lifeguard.