Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc stuff + bytes glue code #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ include = [
]

[dependencies]

[dependencies.bytes]
version = ">= 0.0.1, < 0.6"
optional = true
40 changes: 30 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
//! }
//!
use std::{cmp, ptr};
use std::io::{self,Write,Read};
use std::iter::repeat;
use std::io::{self, Write, Read};

/// the Buffer contains the underlying memory and data positions
///
Expand All @@ -70,13 +69,11 @@ pub struct Buffer {
impl Buffer {
/// allocates a new buffer of maximum size `capacity`
pub fn with_capacity(capacity: usize) -> Buffer {
let mut v = Vec::with_capacity(capacity);
v.extend(repeat(0).take(capacity));
Buffer {
memory: v,
capacity: capacity,
memory: vec![0; capacity],
capacity,
position: 0,
end: 0
end: 0
}
}

Expand Down Expand Up @@ -259,7 +256,7 @@ impl Buffer {
ptr::copy(data.as_ptr(), (&mut self.memory[begin..slice_end]).as_mut_ptr(), data_len);

ptr::copy((&self.memory[start+length..self.end]).as_ptr(), (&mut self.memory[slice_end..]).as_mut_ptr(), self.end - (start + length));
self.end = self.end - (length - data_len);
self.end -= length - data_len;

// we put more data in the buffer
} else {
Expand All @@ -285,7 +282,7 @@ impl Buffer {
let slice_end = begin + data_len;
ptr::copy((&self.memory[start..self.end]).as_ptr(), (&mut self.memory[start+data_len..]).as_mut_ptr(), self.end - start);
ptr::copy(data.as_ptr(), (&mut self.memory[begin..slice_end]).as_mut_ptr(), data_len);
self.end = self.end + data_len;
self.end += data_len;
}
Some(self.available_data())
}
Expand Down Expand Up @@ -315,6 +312,30 @@ impl Read for Buffer {
}
}

#[cfg(features = "bytes")]
impl bytes::Buf for Buffer {
#[inline]
fn remaining(&self) -> usize {
self.available_data()
}

#[inline]
fn bytes(&self) -> &[u8] {
self.data()
}

#[inline]
fn advance(&mut self, cnt: usize) {
self.consume(cnt)
}
}

// we can't support bytes::BufMut because the interface isn't
// completely sound and also works with uninitialized buffers,
// which isn't compatible with circular::Buffer.
// additionally, it would require at least >= bytes-0.4.0
// see also: https://github.com/tokio-rs/bytes/issues/328

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -394,7 +415,6 @@ mod tests {
assert_eq!(b.data(), &b"ab123Zgh"[..]);
}

use std::str;
#[test]
fn set_position() {
let mut output = [0;5];
Expand Down