Crates.io | kitty_pool |
lib.rs | kitty_pool |
version | 0.3.0 |
source | src |
created_at | 2019-07-10 06:34:26.066527 |
updated_at | 2020-03-09 21:04:17.279374 |
description | A safe buffer pool |
homepage | |
repository | |
max_upload_size | |
id | 148028 |
size | 79,841 |
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. The 'Kitty Pool' also allows for a standard scatter gather list implementation.
use kitty_pool::*;
const POOL_SIZE: usize = 1024;
const BLOCK_SIZE: usize = 64;
const REQUEST_SIZE: usize = 256;
fn main() {
// Supports Contiguous and ScatterGatherList
let result = kitty_pool(KittyPoolOrganization::Contiguous, POOL_SIZE, BLOCK_SIZE);
assert!(result.is_ok());
let mut kitty_pool = result.unwrap();
let rand_string: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(REQUEST_SIZE)
.collect();
let rand_bytes: Vec<u8> = Vec::from(rand_string.as_bytes());
let mut buffer = vec![0; REQUEST_SIZE];
let future = async {
let mut token = kitty_pool.borrow(REQUEST_SIZE).await;
assert!(token.is_ok());
token = kitty_pool.write(&token.unwrap(), &rand_bytes).await;
assert!(token.is_ok());
token = kitty_pool.read(&token.unwrap(), &mut buffer).await;
assert!(token.is_ok());
assert_eq!(*buffer, *rand_bytes);
assert!(kitty_pool.release(&token.unwrap()).is_ok());
};
block_on(future);
}