From 38abb8074b87f226c32213ebb1bee7752b43278c Mon Sep 17 00:00:00 2001 From: Carl Lerche <me@carllerche.com> Date: Tue, 23 Aug 2016 14:22:39 -0700 Subject: [PATCH] Create ByteBuf with MutByteBuf::with_capacity --- src/buf/byte.rs | 12 ++++++------ src/bytes/rope.rs | 4 ++-- src/bytes/seq.rs | 4 ++-- test/test_buf_fill.rs | 2 +- test/test_byte_buf.rs | 8 ++++---- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/buf/byte.rs b/src/buf/byte.rs index 1331e78..2b14744 100644 --- a/src/buf/byte.rs +++ b/src/buf/byte.rs @@ -22,16 +22,11 @@ pub struct ByteBuf { impl ByteBuf { /// Create a new `ByteBuf` by copying the contents of the given slice. pub fn from_slice(bytes: &[u8]) -> ByteBuf { - let mut buf = ByteBuf::mut_with_capacity(bytes.len()); + let mut buf = MutByteBuf::with_capacity(bytes.len()); buf.write_slice(bytes); buf.flip() } - pub fn mut_with_capacity(capacity: usize) -> MutByteBuf { - assert!(capacity <= MAX_CAPACITY); - MutByteBuf { buf: ByteBuf::new(capacity as u32) } - } - 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); @@ -180,6 +175,11 @@ pub struct MutByteBuf { } 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 } diff --git a/src/bytes/rope.rs b/src/bytes/rope.rs index 4d9a20e..3eb7948 100644 --- a/src/bytes/rope.rs +++ b/src/bytes/rope.rs @@ -1,4 +1,4 @@ -use {Bytes, ByteBuf}; +use {Bytes, MutByteBuf}; use buf::{Buf, MutBuf, Source}; use bytes::seq::Seq; use bytes::small::{Small}; @@ -351,7 +351,7 @@ impl ops::Index<usize> for Node { fn concat_bytes<S1, S2>(left: S1, right: S2, len: usize) -> Bytes where S1: Source, S2: Source, { - let mut buf = ByteBuf::mut_with_capacity(len); + let mut buf = MutByteBuf::with_capacity(len); buf.copy_from(left); buf.copy_from(right); diff --git a/src/bytes/seq.rs b/src/bytes/seq.rs index fdc82c0..e6a1d33 100644 --- a/src/bytes/seq.rs +++ b/src/bytes/seq.rs @@ -1,6 +1,6 @@ //! Immutable set of bytes sequential in memory. -use {alloc, ByteBuf, MutBuf}; +use {alloc, MutByteBuf, MutBuf}; use bytes::{Bytes}; use std::ops; use std::io::Cursor; @@ -13,7 +13,7 @@ pub struct Seq { impl Seq { pub fn from_slice(bytes: &[u8]) -> Bytes { - let mut buf = ByteBuf::mut_with_capacity(bytes.len()); + let mut buf = MutByteBuf::with_capacity(bytes.len()); buf.copy_from(bytes); buf.flip().into() diff --git a/test/test_buf_fill.rs b/test/test_buf_fill.rs index 79ed48d..05ab308 100644 --- a/test/test_buf_fill.rs +++ b/test/test_buf_fill.rs @@ -4,7 +4,7 @@ use std::io; #[test] pub fn test_readijng_buf_from_reader() { let mut reader = chunks(vec![b"foo", b"bar", b"baz"]); - let mut buf = ByteBuf::mut_with_capacity(1024); + let mut buf = MutByteBuf::with_capacity(1024); assert_eq!(3, reader.read_buf(&mut buf).unwrap()); assert_eq!(Bytes::from(&b"foo"), Bytes::from(buf.flip())); diff --git a/test/test_byte_buf.rs b/test/test_byte_buf.rs index 93e5453..34f596a 100644 --- a/test/test_byte_buf.rs +++ b/test/test_byte_buf.rs @@ -1,9 +1,9 @@ use bytes::{Buf, MutBuf}; -use bytes::ByteBuf; +use bytes::MutByteBuf; #[test] pub fn test_initial_buf_empty() { - let buf = ByteBuf::mut_with_capacity(100); + let buf = MutByteBuf::with_capacity(100); assert!(buf.capacity() == 128); assert!(buf.remaining() == 128); @@ -19,7 +19,7 @@ pub fn test_initial_buf_empty() { #[test] pub fn test_byte_buf_bytes() { - let mut buf = ByteBuf::mut_with_capacity(32); + let mut buf = MutByteBuf::with_capacity(32); buf.copy_from(&b"hello "[..]); assert_eq!(&b"hello "[..], buf.bytes()); @@ -31,7 +31,7 @@ pub fn test_byte_buf_bytes() { #[test] pub fn test_byte_buf_read_write() { - let mut buf = ByteBuf::mut_with_capacity(32); + let mut buf = MutByteBuf::with_capacity(32); buf.copy_from(&b"hello world"[..]); assert_eq!(21, buf.remaining()); -- GitLab