Fix a couple of bugs

This commit is contained in:
Carl Lerche
2015-03-09 22:21:10 -07:00
parent b7c7ff5ab6
commit 113bff22c6
2 changed files with 10 additions and 6 deletions
+2 -1
View File
@@ -1,8 +1,9 @@
use std::{mem, ptr};
use std::rt::heap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::usize;
const MAX_ALLOC_SIZE: usize = (1 << 32) - 1;
const MAX_ALLOC_SIZE: usize = usize::MAX;
/// Allocates memory to be used by Bufs or Bytes. Allows allocating memory
/// using alternate stratgies than the default Rust heap allocator. Also does
+8 -5
View File
@@ -307,25 +307,28 @@ impl<'a> Sink for &'a mut Vec<u8> {
fn sink<B: Buf>(self, buf: &mut B) -> Result<usize, BufError> {
use std::slice;
self.clear();
let rem = buf.remaining();
let cap = self.capacity();
let len = rem - cap;
// Ensure that the vec is big enough
if cap < rem {
self.reserve(len);
if rem > self.capacity() {
self.reserve(rem - cap);
}
unsafe {
{
let dst = self.as_mut_slice();
buf.read_slice(slice::from_raw_parts_mut(dst.as_mut_ptr(), rem));
let cnt = buf.read_slice(slice::from_raw_parts_mut(dst.as_mut_ptr(), rem));
debug_assert!(cnt == rem);
}
self.set_len(rem);
}
Ok(len)
Ok(rem)
}
}