refactor(http): add From<Vec<u8>> impl for MemBuf

This commit is contained in:
Sean McArthur
2017-01-25 11:05:58 -08:00
parent e3ef866a31
commit 44f2bc6d00

View File

@@ -176,6 +176,18 @@ impl fmt::Debug for MemBuf {
} }
} }
impl From<Vec<u8>> for MemBuf {
fn from(mut vec: Vec<u8>) -> MemBuf {
let end = vec.iter().find(|&&x| x == 0).map(|&x| x as usize).unwrap_or(vec.len());
vec.shrink_to_fit();
MemBuf {
buf: Arc::new(UnsafeCell::new(vec)),
start: 0,
end: end,
}
}
}
pub struct MemSlice { pub struct MemSlice {
buf: Arc<UnsafeCell<Vec<u8>>>, buf: Arc<UnsafeCell<Vec<u8>>>,
start: usize, start: usize,
@@ -284,8 +296,7 @@ mod tests {
#[test] #[test]
fn test_mem_slice_slice() { fn test_mem_slice_slice() {
let mut buf = MemBuf::with_capacity(32); let mut buf = MemBuf::from(b"Hello World".to_vec());
buf.read_from(&mut &b"Hello World"[..]).unwrap();
let len = buf.len(); let len = buf.len();
let full = buf.slice(len); let full = buf.slice(len);