kitty_pool 0.1.0

A safe buffer pool
Documentation

Kitty Pool

Purpose

The goal of this crate is to provide a safe and easy to use buffer pool in both a single-threaded and multi-threaded environment. The 'Kitty Pool' allows for requesting of 'Ranges', which act as owned slices into the buffer. The user of the 'Ranges' can then safely read and write to the owned parts of the buffer.

Example

use lifeguard::*;

fn main() {
    let lifeguard = LifeGuard::new(
        1024 * 1024 * 100, // size of buffer to use
        4 * 1024 * 1024,   // size of fragments in buffer, must be divisible into the buffer
    );

    // Simple random data buffer
    let rand_string: String = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(16 * 1024 * 1024)
        .collect();
    let write_buffer = SharedLifeGuardBuffer::new(Mutex::new(rand_string.into_bytes()));

    // same size read buffer to fill
    let read_buffer = SharedLifeGuardBuffer::new(Mutex::new(vec![0; 16 * 1024 * 1024]));

    let result = lifeguard
        .borrow_ranges(16 * 1024 * 1024)
        .and_then(move |mut lifesaver| {
            // After obtaining ranges in buffer, can read and write into buffer
            let write_result = lifesaver.write(write_buffer.lock().unwrap().as_slice());
            assert!(write_result.is_ok());
            let read_result = lifesaver.read(read_buffer.lock().unwrap().as_mut_slice());
            assert!(read_result.is_ok());
            assert_eq!(write_result.unwrap(), read_result.unwrap());
            ok(())
        })
        .poll();
    assert!(result.is_ok());
    assert_eq!(write_buffer, read_buffer);
}

TODO

  • Filter pattern, should allow for using arbitrary segments of the owned ranges
  • Flag for contiguousness