Skip to content

Commit 44f2bc6

Browse files
committed
refactor(http): add From<Vec<u8>> impl for MemBuf
1 parent e3ef866 commit 44f2bc6

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/http/buf.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ impl fmt::Debug for MemBuf {
176176
}
177177
}
178178

179+
impl From<Vec<u8>> for MemBuf {
180+
fn from(mut vec: Vec<u8>) -> MemBuf {
181+
let end = vec.iter().find(|&&x| x == 0).map(|&x| x as usize).unwrap_or(vec.len());
182+
vec.shrink_to_fit();
183+
MemBuf {
184+
buf: Arc::new(UnsafeCell::new(vec)),
185+
start: 0,
186+
end: end,
187+
}
188+
}
189+
}
190+
179191
pub struct MemSlice {
180192
buf: Arc<UnsafeCell<Vec<u8>>>,
181193
start: usize,
@@ -284,8 +296,7 @@ mod tests {
284296

285297
#[test]
286298
fn test_mem_slice_slice() {
287-
let mut buf = MemBuf::with_capacity(32);
288-
buf.read_from(&mut &b"Hello World"[..]).unwrap();
299+
let mut buf = MemBuf::from(b"Hello World".to_vec());
289300

290301
let len = buf.len();
291302
let full = buf.slice(len);

0 commit comments

Comments
 (0)