3个版本
0.1.2 | 2019年9月10日 |
---|---|
0.1.1 | 2017年7月24日 |
0.1.0 | 2017年7月24日 |
#1781 在 数据结构
16KB
293 行
双缓冲区
Simon Cooke的Bip-Buffer的Rust实现
Bip-Buffer类似于循环缓冲区,但数据插入到缓冲区空间的两个旋转区域。这允许读取返回连续的内存块,即使它们跨越一个在循环缓冲区中通常包含环绕的区域。它对于需要连续内存块的API特别有用,消除了在使用前将数据复制到临时缓冲区的需要。
示例
use bipbuffer::BipBuffer;
// Creates a 4-element Bip-Buffer of u8
let mut buffer: BipBuffer<u8> = BipBuffer::new(4);
{
// Reserves 4 slots for insert
let reserved = buffer.reserve(4).unwrap();
reserved[0] = 7;
reserved[1] = 22;
reserved[2] = 218;
reserved[3] = 56;
}
// Stores the values into an available region,
// clearing the existing reservation
buffer.commit(4);
{
// Gets the data stored in the region as a contiguous block
let block = buffer.read().unwrap();
assert_eq!(block[0], 7);
assert_eq!(block[1], 22);
assert_eq!(block[2], 218);
assert_eq!(block[3], 56);
}
// Marks the first two parts of the block as free
buffer.decommit(2);
{
// The block should now contain only the last two values
let block = buffer.read().unwrap();
assert_eq!(block[0], 218);
assert_eq!(block[1], 56);
}