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, 4 * 1024 * 1024, );
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()));
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| {
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