Reorganize crate

This commit is contained in:
Carl Lerche
2016-09-23 12:05:32 -07:00
parent d05bfb6346
commit 98e0d954b5
19 changed files with 54 additions and 65 deletions
+113
View File
@@ -0,0 +1,113 @@
use {alloc, MutBuf, Bytes};
use std::cell::Cell;
/// A `Buf` backed by a contiguous region of memory.
///
/// This buffer can only be written to once. Byte strings (immutable views) can
/// be created at any time, not just when the writing is complete.
pub struct AppendBuf {
mem: alloc::MemRef,
rd: Cell<u32>, // Read cursor
wr: u32, // Write cursor
cap: u32,
}
impl AppendBuf {
pub fn with_capacity(mut capacity: u32) -> AppendBuf {
// Round the capacity to the closest power of 2
capacity = capacity.next_power_of_two();
unsafe {
// Allocate the memory
let mem = alloc::heap(capacity as usize);
AppendBuf::from_mem_ref(mem, capacity, 0)
}
}
pub unsafe fn from_mem_ref(mem: alloc::MemRef, cap: u32, pos: u32) -> AppendBuf {
AppendBuf {
mem: mem,
rd: Cell::new(pos),
wr: pos,
cap: cap,
}
}
#[inline]
pub fn len(&self) -> usize {
(self.wr - self.rd.get()) as usize
}
#[inline]
pub fn capacity(&self) -> usize {
(self.cap - self.rd.get()) as usize
}
pub fn bytes(&self) -> &[u8] {
let rd = self.rd.get() as usize;
let wr = self.wr as usize;
unsafe { &self.mem.bytes_slice(rd, wr) }
}
pub fn shift(&self, n: usize) -> Bytes {
let ret = self.slice(0, n);
self.rd.set(self.rd.get() + ret.len() as u32);
assert!(self.rd.get() <= self.wr, "buffer overflow");
ret
}
pub fn drop(&self, n: usize) {
assert!(n <= self.len());
self.rd.set(self.rd.get() + n as u32);
}
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
// TODO: Fix overflow potential
let rd = self.rd.get();
let wr = self.wr;
let begin = begin as u32 + rd;
let end = end as u32 + rd;
assert!(begin <= end && end <= wr, "invalid range");
unsafe { Bytes::from_mem_ref(self.mem.clone(), begin, end - begin) }
}
}
impl MutBuf for AppendBuf {
#[inline]
fn remaining(&self) -> usize {
(self.cap - self.wr) as usize
}
#[inline]
fn has_remaining(&self) -> bool {
// Implemented as an equality for the perfz
self.cap != self.wr
}
#[inline]
unsafe fn advance(&mut self, cnt: usize) {
self.wr += cnt as u32;
if self.wr > self.cap {
panic!("buffer overflow");
}
}
#[inline]
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
let wr = self.wr as usize;
let cap = self.cap as usize;
self.mem.mut_bytes_slice(wr, cap)
}
}
impl AsRef<[u8]> for AppendBuf {
fn as_ref(&self) -> &[u8] {
self.bytes()
}
}
+367
View File
@@ -0,0 +1,367 @@
#![allow(warnings)]
use {alloc, Buf, MutBuf, Bytes};
use buf::AppendBuf;
use std::{cmp, ptr, slice};
use std::io::Cursor;
use std::rc::Rc;
use std::collections::{vec_deque, VecDeque};
/// Append only buffer backed by a chain of `AppendBuf` buffers.
///
/// Each `AppendBuf` block is of a fixed size and allocated on demand. This
/// makes the total capacity of a `BlockBuf` potentially much larger than what
/// is currently allocated.
pub struct BlockBuf {
len: usize,
cap: usize,
blocks: VecDeque<AppendBuf>,
new_block: NewBlock,
}
enum NewBlock {
Heap(usize),
// Pool(Rc<Pool>),
}
pub struct BlockBufCursor<'a> {
rem: usize,
blocks: vec_deque::Iter<'a, AppendBuf>,
curr: Option<Cursor<&'a [u8]>>,
}
// TODO:
//
// - Add `comapct` fn which moves all buffered data into one block.
// - Add `slice` fn which returns `Bytes` for arbitrary views into the Buf
//
impl BlockBuf {
/// Create BlockBuf
pub fn new(max_blocks: usize, block_size: usize) -> BlockBuf {
assert!(max_blocks > 1, "at least 2 blocks required");
let new_block = NewBlock::Heap(block_size);
BlockBuf {
len: 0,
cap: max_blocks * new_block.block_size(),
blocks: VecDeque::with_capacity(max_blocks),
new_block: new_block,
}
}
/// Returns the number of buffered bytes
#[inline]
pub fn len(&self) -> usize {
debug_assert_eq!(self.len, self.blocks.iter().map(|b| b.len()).fold(0, |a, b| a+b));
self.len
}
/// Returns true if there are no buffered bytes
#[inline]
pub fn is_empty(&self) -> bool {
return self.len() == 0
}
/// Returns a `Buf` for the currently buffered bytes.
#[inline]
pub fn buf(&self) -> BlockBufCursor {
let mut iter = self.blocks.iter();
// Get the next leaf node buffer
let block = iter.next()
.map(|block| Cursor::new(block.bytes()));
BlockBufCursor {
rem: self.len(),
blocks: iter,
curr: block,
}
}
/// Consumes `n` buffered bytes, returning them as an immutable `Bytes`
/// value.
///
/// # Panics
///
/// Panics if `n` is greater than the number of buffered bytes.
#[inline]
pub fn shift(&mut self, n: usize) -> Bytes {
trace!("BlockBuf::shift; n={}", n);
// Fast path
match self.blocks.len() {
0 => {
assert!(n == 0, "buffer overflow");
Bytes::empty()
}
1 => {
let (ret, pop) = {
let block = self.blocks.front().expect("unexpected state");
let ret = block.shift(n);
self.len -= n;
(ret, self.len == 0 && !MutBuf::has_remaining(block))
};
if pop {
let _ = self.blocks.pop_front();
}
ret
}
_ => {
self.shift_multi(n)
}
}
}
fn shift_multi(&mut self, mut n: usize) -> Bytes {
let mut ret: Option<Bytes> = None;
while n > 0 {
if !self.have_buffered_data() {
panic!("shift len out of buffered range");
}
let (segment, pop) = {
let block = self.blocks.front().expect("unexpected state");
let block_len = block.len();
let segment_n = cmp::min(n, block_len);
n -= segment_n;
self.len -= segment_n;
let pop = block_len == segment_n && !MutBuf::has_remaining(block);
(block.shift(segment_n), pop)
};
if pop {
let _ = self.blocks.pop_front();
}
ret = Some(match ret.take() {
Some(curr) => {
curr.concat(&segment)
}
None => segment,
});
}
ret.unwrap_or_else(|| Bytes::empty())
}
/// Drop the first `n` buffered bytes
///
/// # Panics
///
/// Panics if `n` is greater than the number of buffered bytes.
pub fn drop(&mut self, mut n: usize) {
while n > 0 {
if !self.have_buffered_data() {
panic!("shift len out of buffered range");
}
let pop = {
let block = self.blocks.front().expect("unexpected state");
let segment_n = cmp::min(n, block.len());
n -= segment_n;
self.len -= segment_n;
block.drop(segment_n);
block.len() == 0
};
if pop {
let _ = self.blocks.pop_front();
}
}
}
pub fn is_compact(&mut self) -> bool {
self.blocks.len() <= 1
}
/// Moves all buffered bytes into a single block.
///
/// # Panics
///
/// Panics if the buffered bytes cannot fit in a single block.
pub fn compact(&mut self) {
trace!("BlockBuf::compact; attempting compaction");
if self.can_compact() {
trace!("BlockBuf::compact; data not aligned at start -- compacting");
let mut compacted = self.new_block.new_block()
.expect("unable to allocate block");
for block in self.blocks.drain(..) {
compacted.write_slice(block.bytes());
}
assert!(self.blocks.is_empty(), "blocks not removed");
self.blocks.push_back(compacted);
}
}
#[inline]
fn can_compact(&self) -> bool {
if self.blocks.len() > 1 {
return true;
}
self.blocks.front()
.map(|b| b.capacity() != self.new_block.block_size())
.unwrap_or(false)
}
/// Return byte slice if bytes are in sequential memory
#[inline]
pub fn bytes(&self) -> Option<&[u8]> {
match self.blocks.len() {
0 => Some(unsafe { slice::from_raw_parts(ptr::null(), 0) }),
1 => self.blocks.front().map(|b| b.bytes()),
_ => None,
}
}
#[inline]
fn block_size(&self) -> usize {
self.new_block.block_size()
}
#[inline]
fn allocate_block(&mut self) {
if let Some(block) = self.new_block.new_block() {
// Store the block
self.blocks.push_back(block);
}
}
#[inline]
fn have_buffered_data(&self) -> bool {
self.len() > 0
}
#[inline]
fn needs_alloc(&self) -> bool {
if let Some(buf) = self.blocks.back() {
// `unallocated_blocks` is checked here because if further blocks
// cannot be allocated, an empty slice should be returned.
if MutBuf::has_remaining(buf) {
return false;
}
}
true
}
}
impl MutBuf for BlockBuf {
#[inline]
fn remaining(&self) -> usize {
// TODO: Ensure that the allocator has enough capacity to provide the
// remaining bytes
self.cap - self.len
}
#[inline]
fn has_remaining(&self) -> bool {
// TODO: Ensure that the allocator has enough capacity to provide the
// remaining bytes
self.cap != self.len
}
unsafe fn advance(&mut self, cnt: usize) {
trace!("BlockBuf::advance; cnt={:?}", cnt);
// `mut_bytes` only returns bytes from the last block, thus it should
// only be possible to advance the last block
if let Some(buf) = self.blocks.back_mut() {
self.len += cnt;
buf.advance(cnt);
}
}
#[inline]
unsafe fn mut_bytes(&mut self) -> &mut [u8] {
if self.needs_alloc() {
if self.blocks.len() != self.blocks.capacity() {
self.allocate_block()
}
}
self.blocks.back_mut()
.map(|buf| buf.mut_bytes())
.unwrap_or(slice::from_raw_parts_mut(ptr::null_mut(), 0))
}
}
impl Default for BlockBuf {
fn default() -> BlockBuf {
BlockBuf::new(16, 8_192)
}
}
impl<'a> Buf for BlockBufCursor<'a> {
fn remaining(&self) -> usize {
self.rem
}
fn bytes(&self) -> &[u8] {
self.curr.as_ref()
.map(|buf| Buf::bytes(buf))
.unwrap_or(unsafe { slice::from_raw_parts(ptr::null(), 0)})
}
fn advance(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.rem);
// Advance the internal cursor
self.rem -= cnt;
// Advance the leaf buffer
while cnt > 0 {
{
let curr = self.curr.as_mut()
.expect("expected a value");
if curr.remaining() > cnt {
curr.advance(cnt);
break;
}
cnt -= curr.remaining();
}
self.curr = self.blocks.next()
.map(|block| Cursor::new(block.bytes()));
}
}
}
impl NewBlock {
#[inline]
fn block_size(&self) -> usize {
match *self {
NewBlock::Heap(size) => size,
// NewBlock::Pool(ref pool) => pool.buffer_len(),
}
}
#[inline]
fn new_block(&self) -> Option<AppendBuf> {
match *self {
NewBlock::Heap(size) => Some(AppendBuf::with_capacity(size as u32)),
// NewBlock::Pool(ref pool) => pool.new_append_buf(),
}
}
}
+240
View File
@@ -0,0 +1,240 @@
use {alloc, Buf, MutBuf, Bytes, MAX_CAPACITY};
use std::{cmp, fmt};
/*
*
* ===== ByteBuf =====
*
*/
/// A `Buf` backed by a contiguous region of memory.
///
/// This `Buf` is better suited for cases where there is a clear delineation
/// between reading and writing.
pub struct ByteBuf {
mem: alloc::MemRef,
cap: u32,
pos: u32,
lim: u32,
mark: Option<u32>,
}
impl ByteBuf {
/// Create a new `ByteBuf` by copying the contents of the given slice.
pub fn from_slice(bytes: &[u8]) -> ByteBuf {
let mut buf = MutByteBuf::with_capacity(bytes.len());
buf.write_slice(bytes);
buf.flip()
}
pub unsafe fn from_mem_ref(mem: alloc::MemRef, cap: u32, pos: u32, lim: u32) -> ByteBuf {
debug_assert!(pos <= lim && lim <= cap, "invalid arguments; cap={}; pos={}; lim={}", cap, pos, lim);
ByteBuf {
mem: mem,
cap: cap,
pos: pos,
lim: lim,
mark: None,
}
}
fn new(mut capacity: u32) -> ByteBuf {
// Round the capacity to the closest power of 2
capacity = capacity.next_power_of_two();
unsafe {
// Allocate the memory
let mem = alloc::heap(capacity as usize);
ByteBuf {
mem: mem,
cap: capacity,
pos: 0,
lim: capacity,
mark: None,
}
}
}
pub fn capacity(&self) -> usize {
self.cap as usize
}
pub fn flip(self) -> MutByteBuf {
let mut buf = MutByteBuf { buf: self };
buf.clear();
buf
}
/// Flips the buffer back to mutable, resetting the write position
/// to the byte after the previous write.
pub fn resume(mut self) -> MutByteBuf {
self.pos = self.lim;
self.lim = self.cap;
MutByteBuf { buf: self }
}
pub fn read_slice(&mut self, dst: &mut [u8]) {
assert!(self.remaining() >= dst.len());
let len = dst.len();
let cnt = len as u32;
let pos = self.pos as usize;
unsafe {
dst.copy_from_slice(&self.mem.bytes()[pos..pos+len]);
}
self.pos += cnt;
}
/// Marks the current read location.
///
/// Together with `reset`, this can be used to read from a section of the
/// buffer multiple times. The marked location will be cleared when the
/// buffer is flipped.
pub fn mark(&mut self) {
self.mark = Some(self.pos);
}
/// Resets the read position to the previously marked position.
///
/// Together with `mark`, this can be used to read from a section of the
/// buffer multiple times.
///
/// # Panics
///
/// This method will panic if no mark has been set.
pub fn reset(&mut self) {
self.pos = self.mark.take().expect("no mark set");
}
#[inline]
fn pos(&self) -> usize {
self.pos as usize
}
#[inline]
fn lim(&self) -> usize {
self.lim as usize
}
#[inline]
fn remaining_u32(&self) -> u32 {
self.lim - self.pos
}
}
impl Buf for ByteBuf {
#[inline]
fn remaining(&self) -> usize {
self.remaining_u32() as usize
}
#[inline]
fn bytes<'a>(&'a self) -> &'a [u8] {
unsafe { &self.mem.bytes()[self.pos()..self.lim()] }
}
#[inline]
fn advance(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.remaining());
self.pos += cnt as u32;
}
#[inline]
fn read_slice(&mut self, dst: &mut [u8]) {
ByteBuf::read_slice(self, dst)
}
}
impl From<ByteBuf> for Bytes {
fn from(src: ByteBuf) -> Bytes {
unsafe {
let ByteBuf { mem, pos, lim, .. } = src;
Bytes::from_mem_ref(mem, pos, lim - pos)
}
}
}
impl fmt::Debug for ByteBuf {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.bytes().fmt(fmt)
}
}
/*
*
* ===== MutByteBuf =====
*
*/
pub struct MutByteBuf {
buf: ByteBuf,
}
impl MutByteBuf {
pub fn with_capacity(capacity: usize) -> MutByteBuf {
assert!(capacity <= MAX_CAPACITY);
MutByteBuf { buf: ByteBuf::new(capacity as u32) }
}
pub fn capacity(&self) -> usize {
self.buf.capacity() as usize
}
pub fn flip(self) -> ByteBuf {
let mut buf = self.buf;
buf.lim = buf.pos;
buf.pos = 0;
buf
}
pub fn clear(&mut self) {
self.buf.pos = 0;
self.buf.lim = self.buf.cap;
}
#[inline]
pub fn write_slice(&mut self, src: &[u8]) -> usize {
let cnt = cmp::min(src.len(), self.buf.remaining());
let pos = self.buf.pos as usize;
unsafe {
self.buf.mem.mut_bytes()[pos..pos+cnt]
.copy_from_slice(&src[0..cnt]);
}
self.buf.pos += cnt as u32;
cnt
}
pub fn bytes<'a>(&'a self) -> &'a [u8] {
unsafe { &self.buf.mem.bytes()[..self.buf.pos()] }
}
}
impl MutBuf for MutByteBuf {
fn remaining(&self) -> usize {
self.buf.remaining()
}
unsafe fn advance(&mut self, cnt: usize) {
self.buf.advance(cnt)
}
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
let pos = self.buf.pos();
let lim = self.buf.lim();
&mut self.buf.mem.mut_bytes()[pos..lim]
}
}
impl fmt::Debug for MutByteBuf {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.bytes().fmt(fmt)
}
}
+676
View File
@@ -0,0 +1,676 @@
pub mod append;
pub mod block;
pub mod byte;
pub mod ring;
pub mod take;
use {Bytes};
use buf::Take;
use byteorder::ByteOrder;
use std::{cmp, fmt, io, ptr, usize};
/// A trait for values that provide sequential read access to bytes.
pub trait Buf {
/// Returns the number of bytes that can be accessed from the Buf
fn remaining(&self) -> usize;
/// Returns a slice starting at the current Buf position and of length
/// between 0 and `Buf::remaining()`.
fn bytes(&self) -> &[u8];
/// Advance the internal cursor of the Buf
fn advance(&mut self, cnt: usize);
/// Returns true if there are any more bytes to consume
fn has_remaining(&self) -> bool {
self.remaining() > 0
}
fn copy_to<S: Sink>(&mut self, dst: S) -> usize
where Self: Sized {
let rem = self.remaining();
dst.copy_from(self);
rem - self.remaining()
}
/// Read bytes from the `Buf` into the given slice and advance the cursor by
/// the number of bytes read.
/// Returns the number of bytes read.
///
/// ```
/// use std::io::Cursor;
/// use bytes::Buf;
///
/// let mut buf = Cursor::new(b"hello world");
/// let mut dst = [0; 5];
///
/// buf.read_slice(&mut dst);
/// assert_eq!(b"hello", &dst);
/// assert_eq!(6, buf.remaining());
/// ```
fn read_slice(&mut self, dst: &mut [u8]) {
let mut off = 0;
assert!(self.remaining() >= dst.len());
while off < dst.len() {
let cnt;
unsafe {
let src = self.bytes();
cnt = cmp::min(src.len(), dst.len() - off);
ptr::copy_nonoverlapping(
src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
off += src.len();
}
self.advance(cnt);
}
}
/// Reads an unsigned 8 bit integer from the `Buf` without advancing the
/// buffer cursor
fn peek_u8(&self) -> Option<u8> {
if self.has_remaining() {
Some(self.bytes()[0])
} else {
None
}
}
/// Reads an unsigned 8 bit integer from the `Buf`.
fn read_u8(&mut self) -> u8 {
let mut buf = [0; 1];
self.read_slice(&mut buf);
buf[0]
}
/// Reads a signed 8 bit integer from the `Buf`.
fn read_i8(&mut self) -> i8 {
let mut buf = [0; 1];
self.read_slice(&mut buf);
buf[0] as i8
}
/// Reads an unsigned 16 bit integer from the `Buf`
fn read_u16<T: ByteOrder>(&mut self) -> u16 {
let mut buf = [0; 2];
self.read_slice(&mut buf);
T::read_u16(&buf)
}
/// Reads a signed 16 bit integer from the `Buf`
fn read_i16<T: ByteOrder>(&mut self) -> i16 {
let mut buf = [0; 2];
self.read_slice(&mut buf);
T::read_i16(&buf)
}
/// Reads an unsigned 32 bit integer from the `Buf`
fn read_u32<T: ByteOrder>(&mut self) -> u32 {
let mut buf = [0; 4];
self.read_slice(&mut buf);
T::read_u32(&buf)
}
/// Reads a signed 32 bit integer from the `Buf`
fn read_i32<T: ByteOrder>(&mut self) -> i32 {
let mut buf = [0; 4];
self.read_slice(&mut buf);
T::read_i32(&buf)
}
/// Reads an unsigned 64 bit integer from the `Buf`
fn read_u64<T: ByteOrder>(&mut self) -> u64 {
let mut buf = [0; 8];
self.read_slice(&mut buf);
T::read_u64(&buf)
}
/// Reads a signed 64 bit integer from the `Buf`
fn read_i64<T: ByteOrder>(&mut self) -> i64 {
let mut buf = [0; 8];
self.read_slice(&mut buf);
T::read_i64(&buf)
}
/// Reads an unsigned n-bytes integer from the `Buf`
fn read_uint<T: ByteOrder>(&mut self, nbytes: usize) -> u64 {
let mut buf = [0; 8];
self.read_slice(&mut buf[..nbytes]);
T::read_uint(&buf[..nbytes], nbytes)
}
/// Reads a signed n-bytes integer from the `Buf`
fn read_int<T: ByteOrder>(&mut self, nbytes: usize) -> i64 {
let mut buf = [0; 8];
self.read_slice(&mut buf[..nbytes]);
T::read_int(&buf[..nbytes], nbytes)
}
/// Reads a IEEE754 single-precision (4 bytes) floating point number from
/// the `Buf`
fn read_f32<T: ByteOrder>(&mut self) -> f32 {
let mut buf = [0; 4];
self.read_slice(&mut buf);
T::read_f32(&buf)
}
/// Reads a IEEE754 double-precision (8 bytes) floating point number from
/// the `Buf`
fn read_f64<T: ByteOrder>(&mut self) -> f64 {
let mut buf = [0; 8];
self.read_slice(&mut buf);
T::read_f64(&buf)
}
/// Creates a "by reference" adaptor for this instance of Buf
fn by_ref(&mut self) -> &mut Self where Self: Sized {
self
}
/// Create an adapter which will limit at most `limit` bytes from it.
fn take(self, limit: usize) -> Take<Self> where Self: Sized {
Take::new(self, limit)
}
/// Return a `Reader` for the value. Allows using a `Buf` as an `io::Read`
fn reader(self) -> Reader<Self> where Self: Sized {
Reader::new(self)
}
}
/// A trait for values that provide sequential write access to bytes.
pub trait MutBuf {
/// Returns the number of bytes that can be written to the MutBuf
fn remaining(&self) -> usize;
/// Advance the internal cursor of the MutBuf
unsafe fn advance(&mut self, cnt: usize);
/// Returns true iff there is any more space for bytes to be written
fn has_remaining(&self) -> bool {
self.remaining() > 0
}
/// Returns a mutable slice starting at the current MutBuf position and of
/// length between 0 and `MutBuf::remaining()`.
///
/// The returned byte slice may represent uninitialized memory.
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8];
fn copy_from<S: Source>(&mut self, src: S) -> usize
where Self: Sized {
let rem = self.remaining();
src.copy_to(self);
rem - self.remaining()
}
/// Write bytes from the given slice into the `MutBuf` and advance the
/// cursor by the number of bytes written.
/// Returns the number of bytes written.
///
/// ```
/// use bytes::MutBuf;
/// use std::io::Cursor;
///
/// let mut dst = [0; 6];
///
/// {
/// let mut buf = Cursor::new(&mut dst);
/// buf.write_slice(b"hello");
///
/// assert_eq!(1, buf.remaining());
/// }
///
/// assert_eq!(b"hello\0", &dst);
/// ```
fn write_slice(&mut self, src: &[u8]) {
let mut off = 0;
assert!(self.remaining() >= src.len(), "buffer overflow");
while off < src.len() {
let cnt;
unsafe {
let dst = self.mut_bytes();
cnt = cmp::min(dst.len(), src.len() - off);
ptr::copy_nonoverlapping(
src[off..].as_ptr(),
dst.as_mut_ptr(),
cnt);
off += cnt;
}
unsafe { self.advance(cnt); }
}
}
fn write_str(&mut self, src: &str) {
self.write_slice(src.as_bytes());
}
/// Writes an unsigned 8 bit integer to the MutBuf.
fn write_u8(&mut self, n: u8) {
self.write_slice(&[n])
}
/// Writes a signed 8 bit integer to the MutBuf.
fn write_i8(&mut self, n: i8) {
self.write_slice(&[n as u8])
}
/// Writes an unsigned 16 bit integer to the MutBuf.
fn write_u16<T: ByteOrder>(&mut self, n: u16) {
let mut buf = [0; 2];
T::write_u16(&mut buf, n);
self.write_slice(&buf)
}
/// Writes a signed 16 bit integer to the MutBuf.
fn write_i16<T: ByteOrder>(&mut self, n: i16) {
let mut buf = [0; 2];
T::write_i16(&mut buf, n);
self.write_slice(&buf)
}
/// Writes an unsigned 32 bit integer to the MutBuf.
fn write_u32<T: ByteOrder>(&mut self, n: u32) {
let mut buf = [0; 4];
T::write_u32(&mut buf, n);
self.write_slice(&buf)
}
/// Writes a signed 32 bit integer to the MutBuf.
fn write_i32<T: ByteOrder>(&mut self, n: i32) {
let mut buf = [0; 4];
T::write_i32(&mut buf, n);
self.write_slice(&buf)
}
/// Writes an unsigned 64 bit integer to the MutBuf.
fn write_u64<T: ByteOrder>(&mut self, n: u64) {
let mut buf = [0; 8];
T::write_u64(&mut buf, n);
self.write_slice(&buf)
}
/// Writes a signed 64 bit integer to the MutBuf.
fn write_i64<T: ByteOrder>(&mut self, n: i64) {
let mut buf = [0; 8];
T::write_i64(&mut buf, n);
self.write_slice(&buf)
}
/// Writes an unsigned n-bytes integer to the MutBuf.
///
/// If the given integer is not representable in the given number of bytes,
/// this method panics. If `nbytes > 8`, this method panics.
fn write_uint<T: ByteOrder>(&mut self, n: u64, nbytes: usize) {
let mut buf = [0; 8];
T::write_uint(&mut buf, n, nbytes);
self.write_slice(&buf[0..nbytes])
}
/// Writes a signed n-bytes integer to the MutBuf.
///
/// If the given integer is not representable in the given number of bytes,
/// this method panics. If `nbytes > 8`, this method panics.
fn write_int<T: ByteOrder>(&mut self, n: i64, nbytes: usize) {
let mut buf = [0; 8];
T::write_int(&mut buf, n, nbytes);
self.write_slice(&buf[0..nbytes])
}
/// Writes a IEEE754 single-precision (4 bytes) floating point number to
/// the MutBuf.
fn write_f32<T: ByteOrder>(&mut self, n: f32) {
let mut buf = [0; 4];
T::write_f32(&mut buf, n);
self.write_slice(&buf)
}
/// Writes a IEEE754 double-precision (8 bytes) floating point number to
/// the MutBuf.
fn write_f64<T: ByteOrder>(&mut self, n: f64) {
let mut buf = [0; 8];
T::write_f64(&mut buf, n);
self.write_slice(&buf)
}
/// Creates a "by reference" adaptor for this instance of MutBuf
fn by_ref(&mut self) -> &mut Self where Self: Sized {
self
}
/// Create an adapter which will limit at most `limit` bytes from it.
fn take(self, limit: usize) -> Take<Self> where Self: Sized {
Take::new(self, limit)
}
/// Return a `Write` for the value. Allows using a `MutBuf` as an
/// `io::Write`
fn writer(self) -> Writer<Self> where Self: Sized {
Writer::new(self)
}
}
/*
*
* ===== Sink / Source =====
*
*/
/// A value that writes bytes from itself into a `MutBuf`.
pub trait Source {
fn copy_to<B: MutBuf>(self, buf: &mut B);
}
impl<'a> Source for &'a [u8] {
fn copy_to<B: MutBuf>(self, buf: &mut B) {
buf.write_slice(self);
}
}
impl Source for u8 {
fn copy_to<B: MutBuf>(self, buf: &mut B) {
let src = [self];
buf.write_slice(&src);
}
}
impl Source for Bytes {
fn copy_to<B: MutBuf>(self, buf: &mut B) {
Source::copy_to(&self, buf);
}
}
impl<'a> Source for &'a Bytes {
fn copy_to<B: MutBuf>(self, buf: &mut B) {
Source::copy_to(self.buf(), buf);
}
}
impl<T: Buf> Source for T {
fn copy_to<B: MutBuf>(mut self, buf: &mut B) {
while self.has_remaining() && buf.has_remaining() {
let l;
unsafe {
let s = self.bytes();
let d = buf.mut_bytes();
l = cmp::min(s.len(), d.len());
ptr::copy_nonoverlapping(
s.as_ptr(),
d.as_mut_ptr(),
l);
}
self.advance(l);
unsafe { buf.advance(l); }
}
}
}
pub trait Sink {
fn copy_from<B: Buf>(self, buf: &mut B);
}
impl<'a> Sink for &'a mut [u8] {
fn copy_from<B: Buf>(self, buf: &mut B) {
buf.read_slice(self);
}
}
impl<'a> Sink for &'a mut Vec<u8> {
fn copy_from<B: Buf>(self, buf: &mut B) {
use std::slice;
self.clear();
let rem = buf.remaining();
// Ensure that the vec is big enough
if rem > self.capacity() {
// current length is 0, so reserve completely
self.reserve(rem);
}
debug_assert!(rem <= self.capacity());
unsafe {
{
let dst = &mut self[..];
buf.read_slice(slice::from_raw_parts_mut(dst.as_mut_ptr(), rem));
}
self.set_len(rem);
}
}
}
/*
*
* ===== Read / Write =====
*
*/
/// Adapts a `Buf` to the `io::Read` trait
pub struct Reader<B> {
buf: B,
}
impl<B: Buf> Reader<B> {
/// Return a `Reader` for the given `buf`
pub fn new(buf: B) -> Reader<B> {
Reader { buf: buf }
}
/// Gets a reference to the underlying buf.
pub fn get_ref(&self) -> &B {
&self.buf
}
/// Gets a mutable reference to the underlying buf.
pub fn get_mut(&mut self) -> &mut B {
&mut self.buf
}
/// Unwraps this `Reader`, returning the underlying `Buf`
pub fn into_inner(self) -> B {
self.buf
}
}
impl<B: Buf + Sized> io::Read for Reader<B> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
let len = cmp::min(self.buf.remaining(), dst.len());
Buf::copy_to(&mut self.buf, &mut dst[0..len]);
Ok(len)
}
}
/// Buffer related extension for `io::Read`
pub trait ReadExt {
fn read_buf<B: MutBuf>(&mut self, buf: &mut B) -> io::Result<usize>;
}
impl<T: io::Read> ReadExt for T {
fn read_buf<B: MutBuf>(&mut self, buf: &mut B) -> io::Result<usize> {
if !buf.has_remaining() {
return Ok(0);
}
unsafe {
let i = try!(self.read(buf.mut_bytes()));
buf.advance(i);
Ok(i)
}
}
}
/// Adapts a `MutBuf` to the `io::Write` trait
pub struct Writer<B> {
buf: B,
}
impl<B: MutBuf> Writer<B> {
/// Return a `Writer` for teh given `buf`
pub fn new(buf: B) -> Writer<B> {
Writer { buf: buf }
}
/// Gets a reference to the underlying buf.
pub fn get_ref(&self) -> &B {
&self.buf
}
/// Gets a mutable reference to the underlying buf.
pub fn get_mut(&mut self) -> &mut B {
&mut self.buf
}
/// Unwraps this `Writer`, returning the underlying `MutBuf`
pub fn into_inner(self) -> B {
self.buf
}
}
impl<B: MutBuf + Sized> io::Write for Writer<B> {
fn write(&mut self, src: &[u8]) -> io::Result<usize> {
let n = cmp::min(self.buf.remaining(), src.len());
self.buf.copy_from(&src[0..n]);
Ok(n)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
/// Buffer related extension for `io::Write`
pub trait WriteExt {
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize>;
}
impl<T: io::Write> WriteExt for T {
fn write_buf<B: Buf>(&mut self, buf: &mut B) -> io::Result<usize> {
if !buf.has_remaining() {
return Ok(0);
}
let i = try!(self.write(buf.bytes()));
buf.advance(i);
Ok(i)
}
}
/*
*
* ===== Buf impls =====
*
*/
impl<T: AsRef<[u8]>> Buf for io::Cursor<T> {
fn remaining(&self) -> usize {
self.get_ref().as_ref().len() - self.position() as usize
}
fn bytes(&self) -> &[u8] {
let pos = self.position() as usize;
&(self.get_ref().as_ref())[pos..]
}
fn advance(&mut self, cnt: usize) {
let pos = self.position() as usize;
let pos = cmp::min(self.get_ref().as_ref().len(), pos + cnt);
self.set_position(pos as u64);
}
}
impl<T: AsMut<[u8]> + AsRef<[u8]>> MutBuf for io::Cursor<T> {
fn remaining(&self) -> usize {
self.get_ref().as_ref().len() - self.position() as usize
}
/// Advance the internal cursor of the MutBuf
unsafe fn advance(&mut self, cnt: usize) {
let pos = self.position() as usize;
let pos = cmp::min(self.get_mut().as_mut().len(), pos + cnt);
self.set_position(pos as u64);
}
/// Returns a mutable slice starting at the current MutBuf position and of
/// length between 0 and `MutBuf::remaining()`.
///
/// The returned byte slice may represent uninitialized memory.
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
let pos = self.position() as usize;
&mut (self.get_mut().as_mut())[pos..]
}
}
impl MutBuf for Vec<u8> {
fn remaining(&self) -> usize {
usize::MAX - self.len()
}
unsafe fn advance(&mut self, cnt: usize) {
let len = self.len() + cnt;
if len > self.capacity() {
// Reserve additional
// TODO: Should this case panic?
let cap = self.capacity();
self.reserve(cap - len);
}
self.set_len(len);
}
unsafe fn mut_bytes(&mut self) -> &mut [u8] {
use std::slice;
if self.capacity() == self.len() {
self.reserve(64); // Grow the vec
}
let cap = self.capacity();
let len = self.len();
let ptr = self.as_mut_ptr();
&mut slice::from_raw_parts_mut(ptr, cap)[len..]
}
}
/*
*
* ===== fmt impls =====
*
*/
pub struct Fmt<'a, B: 'a>(pub &'a mut B);
impl<'a, B: MutBuf> fmt::Write for Fmt<'a, B> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.0.write_str(s);
Ok(())
}
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
fmt::write(self, args)
}
}
+187
View File
@@ -0,0 +1,187 @@
use {alloc, Buf, MutBuf};
use std::{cmp, fmt};
enum Mark {
NoMark,
At { pos: usize, len: usize },
}
/// Buf backed by a continous chunk of memory. Maintains a read cursor and a
/// write cursor. When reads and writes reach the end of the allocated buffer,
/// wraps around to the start.
///
/// This type is suited for use cases where reads and writes are intermixed.
pub struct RingBuf {
ptr: alloc::MemRef, // Pointer to the memory
cap: usize, // Capacity of the buffer
pos: usize, // Offset of read cursor
len: usize, // Number of bytes to read
mark: Mark, // Marked read position
}
// TODO: There are most likely many optimizations that can be made
impl RingBuf {
/// Allocates a new `RingBuf` with the specified capacity.
pub fn with_capacity(mut capacity: usize) -> RingBuf {
// Round to the next power of 2 for better alignment
capacity = capacity.next_power_of_two();
unsafe {
let mem = alloc::heap(capacity as usize);
RingBuf {
ptr: mem,
cap: capacity,
pos: 0,
len: 0,
mark: Mark::NoMark,
}
}
}
/// Returns `true` if the buf cannot accept any further writes.
pub fn is_full(&self) -> bool {
self.cap == self.len
}
/// Returns `true` if the buf cannot accept any further reads.
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Returns the number of bytes that the buf can hold.
pub fn capacity(&self) -> usize {
self.cap
}
/// Marks the current read location.
///
/// Together with `reset`, this can be used to read from a section of the
/// buffer multiple times. The mark will be cleared if it is overwritten
/// during a write.
pub fn mark(&mut self) {
self.mark = Mark::At { pos: self.pos, len: self.len };
}
/// Resets the read position to the previously marked position.
///
/// Together with `mark`, this can be used to read from a section of the
/// buffer multiple times.
///
/// # Panics
///
/// This method will panic if no mark has been set,
pub fn reset(&mut self){
match self.mark {
Mark::NoMark => panic!("no mark set"),
Mark::At {pos, len} => {
self.pos = pos;
self.len = len;
self.mark = Mark::NoMark;
}
}
}
/// Resets all internal state to the initial state.
pub fn clear(&mut self) {
self.pos = 0;
self.len = 0;
self.mark = Mark::NoMark;
}
/// Returns the number of bytes remaining to read.
fn read_remaining(&self) -> usize {
self.len
}
/// Returns the remaining write capacity until which the buf becomes full.
fn write_remaining(&self) -> usize {
self.cap - self.len
}
fn advance_reader(&mut self, mut cnt: usize) {
if self.cap == 0 {
return;
}
cnt = cmp::min(cnt, self.read_remaining());
self.pos += cnt;
self.pos %= self.cap;
self.len -= cnt;
}
fn advance_writer(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.write_remaining());
self.len += cnt;
// Adjust the mark to account for bytes written.
if let Mark::At { ref mut len, .. } = self.mark {
*len += cnt;
}
// Clear the mark if we've written past it.
if let Mark::At { len, .. } = self.mark {
if len > self.cap {
self.mark = Mark::NoMark;
}
}
}
}
impl fmt::Debug for RingBuf {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "RingBuf[.. {}]", self.len)
}
}
impl Buf for RingBuf {
fn remaining(&self) -> usize {
self.read_remaining()
}
fn bytes(&self) -> &[u8] {
let mut to = self.pos + self.len;
if to > self.cap {
to = self.cap
}
unsafe { &self.ptr.bytes()[self.pos .. to] }
}
fn advance(&mut self, cnt: usize) {
self.advance_reader(cnt)
}
}
impl MutBuf for RingBuf {
fn remaining(&self) -> usize {
self.write_remaining()
}
unsafe fn advance(&mut self, cnt: usize) {
self.advance_writer(cnt)
}
unsafe fn mut_bytes(&mut self) -> &mut [u8] {
if self.cap == 0 {
return self.ptr.mut_bytes();
}
let mut from;
let mut to;
from = self.pos + self.len;
from %= self.cap;
to = from + <Self as MutBuf>::remaining(&self);
if to >= self.cap {
to = self.cap;
}
&mut self.ptr.mut_bytes()[from..to]
}
}
unsafe impl Send for RingBuf { }
+69
View File
@@ -0,0 +1,69 @@
use {Buf, MutBuf};
use std::{cmp};
#[derive(Debug)]
pub struct Take<T> {
inner: T,
limit: usize,
}
impl<T> Take<T> {
pub fn new(inner: T, limit: usize) -> Take<T> {
Take {
inner: inner,
limit: limit,
}
}
pub fn into_inner(self) -> T {
self.inner
}
pub fn get_ref(&self) -> &T {
&self.inner
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn limit(&self) -> usize {
self.limit
}
pub fn set_limit(&mut self, lim: usize) {
self.limit = lim
}
}
impl<T: Buf> Buf for Take<T> {
fn remaining(&self) -> usize {
cmp::min(self.inner.remaining(), self.limit)
}
fn bytes<'a>(&'a self) -> &'a [u8] {
&self.inner.bytes()[..self.limit]
}
fn advance(&mut self, cnt: usize) {
let cnt = cmp::min(cnt, self.limit);
self.limit -= cnt;
self.inner.advance(cnt);
}
}
impl<T: MutBuf> MutBuf for Take<T> {
fn remaining(&self) -> usize {
cmp::min(self.inner.remaining(), self.limit)
}
unsafe fn mut_bytes<'a>(&'a mut self) -> &'a mut [u8] {
&mut self.inner.mut_bytes()[..self.limit]
}
unsafe fn advance(&mut self, cnt: usize) {
let cnt = cmp::min(cnt, self.limit);
self.limit -= cnt;
self.inner.advance(cnt);
}
}
+243
View File
@@ -0,0 +1,243 @@
mod rope;
mod seq;
mod small;
use {alloc, Buf};
use self::seq::Seq;
use self::small::Small;
use self::rope::{Rope, RopeBuf};
use std::{cmp, fmt, ops};
use std::io::Cursor;
use std::sync::Arc;
#[derive(Clone)]
pub struct Bytes {
kind: Kind,
}
#[derive(Clone)]
enum Kind {
Seq(Seq),
Small(Small),
Rope(Arc<Rope>),
}
pub struct BytesBuf<'a> {
kind: BufKind<'a>,
}
enum BufKind<'a> {
Cursor(Cursor<&'a [u8]>),
Rope(RopeBuf<'a>),
}
impl Bytes {
/// Return an empty `Bytes`
pub fn empty() -> Bytes {
Bytes { kind: Kind::Small(Small::empty()) }
}
/// Creates a new `Bytes` from a `MemRef`, an offset, and a length.
///
/// This function is unsafe as there are no guarantees that the given
/// arguments are valid.
#[inline]
pub unsafe fn from_mem_ref(mem: alloc::MemRef, pos: u32, len: u32) -> Bytes {
Small::from_slice(&mem.bytes_slice(pos as usize, pos as usize + len as usize))
.map(|b| Bytes { kind: Kind::Small(b) })
.unwrap_or_else(|| {
let seq = Seq::from_mem_ref(mem, pos, len);
Bytes { kind: Kind::Seq(seq) }
})
}
pub fn buf(&self) -> BytesBuf {
let kind = match self.kind {
Kind::Seq(ref v) => BufKind::Cursor(v.buf()),
Kind::Small(ref v) => BufKind::Cursor(v.buf()),
Kind::Rope(ref v) => BufKind::Rope(v.buf()),
};
BytesBuf { kind: kind }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
match self.kind {
Kind::Seq(ref v) => v.len(),
Kind::Small(ref v) => v.len(),
Kind::Rope(ref v) => v.len(),
}
}
pub fn concat(&self, other: &Bytes) -> Bytes {
Rope::concat(self.clone(), other.clone())
}
/// Returns a new ByteStr value containing the byte range between `begin`
/// (inclusive) and `end` (exclusive)
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
match self.kind {
Kind::Seq(ref v) => v.slice(begin, end),
Kind::Small(ref v) => v.slice(begin, end),
Kind::Rope(ref v) => v.slice(begin, end),
}
}
/// Returns a new ByteStr value containing the byte range starting from
/// `begin` (inclusive) to the end of the byte str.
///
/// Equivalent to `bytes.slice(begin, bytes.len())`
pub fn slice_from(&self, begin: usize) -> Bytes {
self.slice(begin, self.len())
}
/// Returns a new ByteStr value containing the byte range from the start up
/// to `end` (exclusive).
///
/// Equivalent to `bytes.slice(0, end)`
pub fn slice_to(&self, end: usize) -> Bytes {
self.slice(0, end)
}
/// Returns the Rope depth
fn depth(&self) -> u16 {
match self.kind {
Kind::Rope(ref r) => r.depth(),
_ => 0,
}
}
fn into_rope(self) -> Result<Arc<Rope>, Bytes> {
match self.kind {
Kind::Rope(r) => Ok(r),
_ => Err(self),
}
}
}
impl ops::Index<usize> for Bytes {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
match self.kind {
Kind::Seq(ref v) => v.index(index),
Kind::Small(ref v) => v.index(index),
Kind::Rope(ref v) => v.index(index),
}
}
}
impl<T: AsRef<[u8]>> From<T> for Bytes {
fn from(src: T) -> Bytes {
Small::from_slice(src.as_ref())
.map(|b| Bytes { kind: Kind::Small(b) })
.unwrap_or_else(|| Seq::from_slice(src.as_ref()))
}
}
impl cmp::PartialEq<Bytes> for Bytes {
fn eq(&self, other: &Bytes) -> bool {
if self.len() != other.len() {
return false;
}
let mut buf1 = self.buf();
let mut buf2 = self.buf();
while buf1.has_remaining() {
let len;
{
let b1 = buf1.bytes();
let b2 = buf2.bytes();
len = cmp::min(b1.len(), b2.len());
if b1[..len] != b2[..len] {
return false;
}
}
buf1.advance(len);
buf2.advance(len);
}
true
}
fn ne(&self, other: &Bytes) -> bool {
return !self.eq(other)
}
}
impl<'a> Buf for BytesBuf<'a> {
fn remaining(&self) -> usize {
match self.kind {
BufKind::Cursor(ref v) => v.remaining(),
BufKind::Rope(ref v) => v.remaining(),
}
}
fn bytes(&self) -> &[u8] {
match self.kind {
BufKind::Cursor(ref v) => v.bytes(),
BufKind::Rope(ref v) => v.bytes(),
}
}
fn advance(&mut self, cnt: usize) {
match self.kind {
BufKind::Cursor(ref mut v) => v.advance(cnt),
BufKind::Rope(ref mut v) => v.advance(cnt),
}
}
}
/*
*
* ===== Internal utilities =====
*
*/
impl fmt::Debug for Bytes {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut buf = self.buf();
try!(write!(fmt, "Bytes[len={}; ", self.len()));
let mut rem = 128;
while buf.has_remaining() {
let byte = buf.read_u8();
if rem > 0 {
if is_ascii(byte) {
try!(write!(fmt, "{}", byte as char));
} else {
try!(write!(fmt, "\\x{:02X}", byte));
}
rem -= 1;
} else {
try!(write!(fmt, " ... "));
break;
}
}
try!(write!(fmt, "]"));
Ok(())
}
}
fn is_ascii(byte: u8) -> bool {
match byte {
10 | 13 | 32...126 => true,
_ => false,
}
}
+642
View File
@@ -0,0 +1,642 @@
use {Buf, MutBuf, Bytes};
use super::seq::Seq;
use super::small::{Small};
use buf::{Source, MutByteBuf};
use std::{cmp, ops};
use std::io::Cursor;
use std::sync::Arc;
// The implementation is mostly a port of the implementation found in the Java
// protobuf lib.
const CONCAT_BY_COPY_LEN: usize = 128;
const MAX_DEPTH: usize = 47;
// Used to decide when to rebalance the tree.
static MIN_LENGTH_BY_DEPTH: [usize; MAX_DEPTH] = [
1, 2, 3, 5, 8,
13, 21, 34, 55, 89,
144, 233, 377, 610, 987,
1_597, 2_584, 4_181, 6_765, 10_946,
17_711, 28_657, 46_368, 75_025, 121_393,
196_418, 317_811, 514_229, 832_040, 1_346_269,
2_178_309, 3_524_578, 5_702_887, 9_227_465, 14_930_352,
24_157_817, 39_088_169, 63_245_986, 102_334_155, 165_580_141,
267_914_296, 433_494_437, 701_408_733, 1_134_903_170, 1_836_311_903,
2_971_215_073, 4_294_967_295];
/// An immutable sequence of bytes formed by concatenation of other `ByteStr`
/// values, without copying the data in the pieces. The concatenation is
/// represented as a tree whose leaf nodes are each a `Bytes` value.
///
/// Most of the operation here is inspired by the now-famous paper [Ropes: an
/// Alternative to Strings. hans-j. boehm, russ atkinson and michael
/// plass](http://www.cs.rit.edu/usr/local/pub/jeh/courses/QUARTERS/FP/Labs/CedarRope/rope-paper.pdf).
///
/// Fundamentally the Rope algorithm represents the collection of pieces as a
/// binary tree. BAP95 uses a Fibonacci bound relating depth to a minimum
/// sequence length, sequences that are too short relative to their depth cause
/// a tree rebalance. More precisely, a tree of depth d is "balanced" in the
/// terminology of BAP95 if its length is at least F(d+2), where F(n) is the
/// n-the Fibonacci number. Thus for depths 0, 1, 2, 3, 4, 5,... we have
/// minimum lengths 1, 2, 3, 5, 8, 13,...
#[derive(Clone)]
pub struct Rope {
left: Node,
right: Node,
depth: u16,
len: usize,
}
pub struct RopeBuf<'a> {
// Number of bytes left to iterate
rem: usize,
// Iterates all the leaf nodes in order
nodes: NodeIter<'a>,
// Current leaf node buffer
leaf_buf: Option<Cursor<&'a [u8]>>,
}
#[derive(Clone)]
enum Node {
Empty,
Seq(Seq),
Small(Small),
Rope(Arc<Rope>),
}
// TODO: store stack inline if possible
struct NodeIter<'a> {
stack: Vec<&'a Rope>,
next: Option<&'a Node>,
}
/// Balance operation state
struct Balance {
stack: Vec<Partial>,
}
/// Temporarily detached branch
enum Partial {
Bytes(Bytes),
Node(Node),
}
impl Rope {
fn new<N1: Into<Node>, N2: Into<Node>>(left: N1, right: N2) -> Rope {
let left = left.into();
let right = right.into();
debug_assert!(!left.is_empty() || right.is_empty());
// If left is 0 then right must be zero
let len = left.len() + right.len();
let depth = cmp::max(left.depth(), right.depth()) + 1;
Rope {
left: left,
right: right,
depth: depth,
len: len,
}
}
pub fn buf(&self) -> RopeBuf {
let mut nodes = NodeIter::new(self);
// Get the next leaf node buffer
let leaf_buf = nodes.next()
.map(|node| node.leaf_buf());
RopeBuf {
rem: self.len(),
nodes: nodes,
leaf_buf: leaf_buf,
}
}
/// Concat two `Bytes` together.
pub fn concat(left: Bytes, right: Bytes) -> Bytes {
if right.is_empty() {
return left;
}
if left.is_empty() {
return right;
}
let len = left.len() + right.len();
if len < CONCAT_BY_COPY_LEN {
return concat_bytes(&left, &right, len);
}
let left = match left.into_rope() {
Ok(left) => {
let len = left.right.len() + right.len();
if len < CONCAT_BY_COPY_LEN {
// Optimization from BAP95: As an optimization of the case
// where the ByteString is constructed by repeated concatenate,
// recognize the case where a short string is concatenated to a
// left-hand node whose right-hand branch is short. In the
// paper this applies to leaves, but we just look at the length
// here. This has the advantage of shedding references to
// unneeded data when substrings have been taken.
//
// When we recognize this case, we do a copy of the data and
// create a new parent node so that the depth of the result is
// the same as the given left tree.
let new_right = concat_bytes(&left.right, &right, len);
return Rope::new(left.left.clone(), new_right).into_bytes();
}
if left.left.depth() > left.right.depth() && left.depth > right.depth() {
// Typically for concatenate-built strings the left-side is
// deeper than the right. This is our final attempt to
// concatenate without increasing the tree depth. We'll redo
// the the node on the RHS. This is yet another optimization
// for building the string by repeatedly concatenating on the
// right.
let new_right = Rope::new(left.right.clone(), right);
return Rope::new(left.left.clone(), new_right).into_bytes();
}
Bytes { kind: super::Kind::Rope(left) }
}
Err(left) => left,
};
// Fine, we'll add a node and increase the tree depth -- unless we
// rebalance ;^)
let depth = cmp::max(left.depth(), right.depth()) + 1;
if len >= MIN_LENGTH_BY_DEPTH[depth as usize] {
// No need to rebalance
return Rope::new(left, right).into_bytes();
}
Balance::new().balance(left, right).into()
}
pub fn depth(&self) -> u16 {
self.depth
}
pub fn len(&self) -> usize {
self.len as usize
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
// Assert args
assert!(begin <= end && end <= self.len(), "invalid range");
let len = end - begin;
// Empty slice
if len == 0 {
return Bytes::empty();
}
// Full rope
if len == self.len() {
return self.clone().into_bytes();
}
// == Proper substring ==
let left_len = self.left.len();
if end <= left_len {
// Slice on the left
return self.left.slice(begin, end);
}
if begin >= left_len {
// Slice on the right
return self.right.slice(begin - left_len, end - left_len);
}
// Split slice
let left_slice = self.left.slice(begin, self.left.len());
let right_slice = self.right.slice(0, end - left_len);
Rope::new(left_slice, right_slice).into_bytes()
}
fn into_bytes(self) -> Bytes {
use super::Kind;
Bytes { kind: Kind::Rope(Arc::new(self)) }
}
}
impl Node {
fn len(&self) -> usize {
match *self {
Node::Seq(ref b) => b.len(),
Node::Small(ref b) => b.len(),
Node::Rope(ref b) => b.len,
Node::Empty => 0,
}
}
fn is_empty(&self) -> bool {
self.len() == 0
}
fn depth(&self) -> u16 {
match *self {
Node::Rope(ref r) => r.depth,
_ => 0,
}
}
fn slice(&self, begin: usize, end: usize) -> Bytes {
match *self {
Node::Seq(ref v) => v.slice(begin, end),
Node::Small(ref v) => v.slice(begin, end),
Node::Rope(ref v) => v.slice(begin, end),
Node::Empty => unreachable!(),
}
}
fn leaf_buf(&self) -> Cursor<&[u8]> {
match *self {
Node::Seq(ref v) => v.buf(),
Node::Small(ref v) => v.buf(),
_ => unreachable!(),
}
}
fn as_rope(&self) -> Option<&Rope> {
match *self {
Node::Rope(ref v) => Some(&**v),
_ => None,
}
}
}
impl<'a> Source for &'a Node {
fn copy_to<B: MutBuf>(self, buf: &mut B) {
match *self {
Node::Seq(ref b) => b.as_slice().copy_to(buf),
Node::Small(ref b) => b.as_ref().copy_to(buf),
Node::Rope(ref b) => b.buf().copy_to(buf),
Node::Empty => unreachable!(),
}
}
}
impl From<Bytes> for Node {
fn from(src: Bytes) -> Node {
use super::Kind;
match src.kind {
Kind::Seq(b) => Node::Seq(b),
Kind::Small(b) => Node::Small(b),
Kind::Rope(b) => Node::Rope(b),
}
}
}
impl From<Rope> for Node {
fn from(src: Rope) -> Node {
Node::Rope(Arc::new(src))
}
}
impl ops::Index<usize> for Rope {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
assert!(index < self.len());
let left_len = self.left.len();
if index < left_len {
self.left.index(index)
} else {
self.right.index(index - left_len)
}
}
}
impl ops::Index<usize> for Node {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
match *self {
Node::Seq(ref v) => v.index(index),
Node::Small(ref v) => v.index(index),
Node::Rope(ref v) => v.index(index),
Node::Empty => unreachable!(),
}
}
}
/*
*
* ===== Helper Fns =====
*
*/
fn concat_bytes<S1, S2>(left: S1, right: S2, len: usize) -> Bytes
where S1: Source, S2: Source,
{
let mut buf = MutByteBuf::with_capacity(len);
buf.copy_from(left);
buf.copy_from(right);
return buf.flip().into();
}
fn depth_for_len(len: usize) -> u16 {
match MIN_LENGTH_BY_DEPTH.binary_search(&len) {
Ok(idx) => idx as u16,
Err(idx) => {
// It wasn't an exact match, so convert to the index of the
// containing fragment, which is one less even than the insertion
// point.
idx as u16 - 1
}
}
}
impl<'a> NodeIter<'a> {
fn new(root: &'a Rope) -> NodeIter<'a> {
let mut iter = NodeIter {
// TODO: Consider allocating with capacity for depth
stack: vec![],
next: None,
};
iter.next = iter.get_leaf_by_left(root);
iter
}
fn get_leaf_by_left(&mut self, mut root: &'a Rope) -> Option<&'a Node> {
loop {
self.stack.push(root);
let left = &root.left;
if left.is_empty() {
return None;
}
if let Some(rope) = left.as_rope() {
root = rope;
continue;
}
return Some(left);
}
}
fn next_non_empty_leaf(&mut self) -> Option<&'a Node>{
loop {
if let Some(rope) = self.stack.pop() {
if let Some(rope) = rope.right.as_rope() {
let res = self.get_leaf_by_left(&rope);
if res.is_none() {
continue;
}
return res;
}
if rope.right.is_empty() {
continue;
}
return Some(&rope.right);
}
return None;
}
}
}
impl<'a> Iterator for NodeIter<'a> {
type Item = &'a Node;
fn next(&mut self) -> Option<&'a Node> {
let ret = self.next.take();
if ret.is_some() {
self.next = self.next_non_empty_leaf();
}
ret
}
}
impl<'a> Buf for RopeBuf<'a> {
fn remaining(&self) -> usize {
self.rem
}
fn bytes(&self) -> &[u8] {
self.leaf_buf.as_ref()
.map(|b| b.bytes())
.unwrap_or(&[])
}
fn advance(&mut self, mut cnt: usize) {
cnt = cmp::min(cnt, self.rem);
// Advance the internal cursor
self.rem -= cnt;
// Advance the leaf buffer
while cnt > 0 {
{
let curr = self.leaf_buf.as_mut()
.expect("expected a value");
if curr.remaining() > cnt {
curr.advance(cnt);
break;
}
cnt -= curr.remaining();
}
self.leaf_buf = self.nodes.next()
.map(|node| node.leaf_buf());
}
}
}
/*
*
* ===== Balance =====
*
*/
impl Balance {
fn new() -> Balance {
Balance { stack: vec![] }
}
fn balance(&mut self, left: Bytes, right: Bytes) -> Bytes {
self.do_balance(Partial::Bytes(left));
self.do_balance(Partial::Bytes(right));
let mut partial = self.stack.pop()
.expect("expected a value");
while !partial.is_empty() {
let new_left = self.stack.pop()
.expect("expected a value");
partial = Partial::Bytes(Rope::new(new_left, partial).into_bytes());
}
partial.unwrap_bytes()
}
fn do_balance(&mut self, root: Partial) {
// BAP95: Insert balanced subtrees whole. This means the result might not
// be balanced, leading to repeated rebalancings on concatenate. However,
// these rebalancings are shallow due to ignoring balanced subtrees, and
// relatively few calls to insert() result.
if root.is_balanced() {
self.insert(root);
} else {
let rope = root.unwrap_rope();
self.do_balance(Partial::Node(rope.left));
self.do_balance(Partial::Node(rope.right));
}
}
// Push a string on the balance stack (BAP95). BAP95 uses an array and
// calls the elements in the array 'bins'. We instead use a stack, so the
// 'bins' of lengths are represented by differences between the elements of
// minLengthByDepth.
//
// If the length bin for our string, and all shorter length bins, are
// empty, we just push it on the stack. Otherwise, we need to start
// concatenating, putting the given string in the "middle" and continuing
// until we land in an empty length bin that matches the length of our
// concatenation.
fn insert(&mut self, bytes: Partial) {
let depth_bin = depth_for_len(bytes.len());
let bin_end = MIN_LENGTH_BY_DEPTH[depth_bin as usize + 1];
// BAP95: Concatenate all trees occupying bins representing the length
// of our new piece or of shorter pieces, to the extent that is
// possible. The goal is to clear the bin which our piece belongs in,
// but that may not be entirely possible if there aren't enough longer
// bins occupied.
if let Some(len) = self.peek().map(|r| r.len()) {
if len >= bin_end {
self.stack.push(bytes);
return;
}
}
let bin_start = MIN_LENGTH_BY_DEPTH[depth_bin as usize];
// Concatenate the subtrees of shorter length
let mut new_tree = self.stack.pop()
.expect("expected a value");
while let Some(len) = self.peek().map(|r| r.len()) {
// If the head is big enough, break the loop
if len >= bin_start { break; }
let left = self.stack.pop()
.expect("expected a value");
new_tree = Partial::Bytes(Rope::new(left, new_tree).into_bytes());
}
// Concatenate the given string
new_tree = Partial::Bytes(Rope::new(new_tree, bytes).into_bytes());
// Continue concatenating until we land in an empty bin
while let Some(len) = self.peek().map(|r| r.len()) {
let depth_bin = depth_for_len(new_tree.len());
let bin_end = MIN_LENGTH_BY_DEPTH[depth_bin as usize + 1];
if len < bin_end {
let left = self.stack.pop()
.expect("expected a value");
new_tree = Partial::Bytes(Rope::new(left, new_tree).into_bytes());
} else {
break;
}
}
self.stack.push(new_tree);
}
fn peek(&self) -> Option<&Partial> {
self.stack.last()
}
}
impl Partial {
fn is_empty(&self) -> bool {
self.len() == 0
}
fn len(&self) -> usize {
match *self {
Partial::Bytes(ref v) => v.len(),
Partial::Node(ref v) => v.len(),
}
}
fn depth(&self) -> u16 {
match *self {
Partial::Bytes(ref v) => v.depth(),
Partial::Node(ref v) => v.depth(),
}
}
fn is_balanced(&self) -> bool {
self.len() >= MIN_LENGTH_BY_DEPTH[self.depth() as usize]
}
fn unwrap_bytes(self) -> Bytes {
match self {
Partial::Bytes(v) => v,
_ => panic!("unexpected state calling `Partial::unwrap_bytes()`. Expected `Bytes`, got `Node`"),
}
}
fn unwrap_rope(self) -> Rope {
let arc = match self {
Partial::Bytes(v) => v.into_rope().ok().expect("unexpected state calling `Partial::unwrap_rope()`"),
Partial::Node(Node::Rope(v)) => v,
_ => panic!("unexpected state calling `Partial::unwrap_rope()`"),
};
match Arc::try_unwrap(arc) {
Ok(v) => v,
Err(v) => (*v).clone(),
}
}
}
impl From<Partial> for Node {
fn from(src: Partial) -> Node {
match src {
Partial::Node(v) => v,
Partial::Bytes(v) => Node::from(v),
}
}
}
+79
View File
@@ -0,0 +1,79 @@
//! Immutable set of bytes sequential in memory.
use {alloc, MutBuf, Bytes};
use buf::{MutByteBuf};
use std::ops;
use std::io::Cursor;
pub struct Seq {
mem: alloc::MemRef,
pos: u32,
len: u32,
}
impl Seq {
pub fn from_slice(bytes: &[u8]) -> Bytes {
let mut buf = MutByteBuf::with_capacity(bytes.len());
buf.copy_from(bytes);
buf.flip().into()
}
/// Creates a new `SeqByteStr` from a `MemRef`, an offset, and a length.
///
/// This function is unsafe as there are no guarantees that the given
/// arguments are valid.
pub unsafe fn from_mem_ref(mem: alloc::MemRef, pos: u32, len: u32) -> Seq {
Seq {
mem: mem,
pos: pos,
len: len,
}
}
pub fn len(&self) -> usize {
self.len as usize
}
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
use super::Kind;
assert!(begin <= end && end <= self.len(), "invalid range");
let seq = unsafe {
Seq::from_mem_ref(
self.mem.clone(),
self.pos + begin as u32,
(end - begin) as u32)
};
Bytes { kind: Kind::Seq(seq) }
}
pub fn buf(&self) -> Cursor<&[u8]> {
Cursor::new(self.as_slice())
}
pub fn as_slice(&self) -> &[u8] {
unsafe { &self.mem.bytes()[self.pos as usize..self.pos as usize + self.len as usize] }
}
}
impl ops::Index<usize> for Seq {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
assert!(index < self.len());
unsafe { self.mem.bytes().index(index + self.pos as usize) }
}
}
impl Clone for Seq {
fn clone(&self) -> Seq {
Seq {
mem: self.mem.clone(),
pos: self.pos,
len: self.len,
}
}
}
+81
View File
@@ -0,0 +1,81 @@
use {Bytes};
use std::ops;
use std::io::Cursor;
/*
*
* ===== Small immutable set of bytes =====
*
*/
#[cfg(target_pointer_width = "64")]
const MAX_LEN: usize = 7;
#[cfg(target_pointer_width = "32")]
const MAX_LEN: usize = 3;
#[derive(Clone, Copy)]
pub struct Small {
len: u8,
bytes: [u8; MAX_LEN],
}
impl Small {
pub fn empty() -> Small {
use std::mem;
Small {
len: 0,
bytes: unsafe { mem::zeroed() }
}
}
pub fn from_slice(bytes: &[u8]) -> Option<Small> {
use std::{mem, ptr};
if bytes.len() > MAX_LEN {
return None;
}
let mut ret = Small {
len: bytes.len() as u8,
bytes: unsafe { mem::zeroed() },
};
// Copy the memory
unsafe {
ptr::copy_nonoverlapping(
bytes.as_ptr(),
ret.bytes.as_mut_ptr(),
bytes.len());
}
Some(ret)
}
pub fn buf(&self) -> Cursor<&[u8]> {
Cursor::new(self.as_ref())
}
pub fn slice(&self, begin: usize, end: usize) -> Bytes {
Bytes::from(&self.as_ref()[begin..end])
}
pub fn len(&self) -> usize {
self.len as usize
}}
impl AsRef<[u8]> for Small {
fn as_ref(&self) -> &[u8] {
&self.bytes[..self.len as usize]
}
}
impl ops::Index<usize> for Small {
type Output = u8;
fn index(&self, index: usize) -> &u8 {
assert!(index < self.len());
&self.bytes[index]
}
}
+4
View File
@@ -0,0 +1,4 @@
//! Used for internal code structure
pub mod buf;
pub mod bytes;