From 2b796d40e9de5b9dafb024af34c4a51358598b77 Mon Sep 17 00:00:00 2001 From: Rick Richardson Date: Mon, 21 Nov 2016 15:43:59 -0800 Subject: [PATCH] added clone to ByteBuf and BytesMut along with simple clone test --- src/buf/byte.rs | 10 ++++++++++ src/bytes.rs | 13 +++++++++++++ tests/test_mut_buf.rs | 13 ++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/buf/byte.rs b/src/buf/byte.rs index 5149b96..b5abfa4 100644 --- a/src/buf/byte.rs +++ b/src/buf/byte.rs @@ -215,3 +215,13 @@ impl fmt::Write for ByteBuf { fmt::write(self, args) } } + +impl Clone for ByteBuf { + fn clone(&self) -> Self { + ByteBuf { + mem: self.mem.clone(), + rd: self.rd, + } + } +} + diff --git a/src/bytes.rs b/src/bytes.rs index c9239a2..e8f8a95 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -558,3 +558,16 @@ impl<'a, T: ?Sized> PartialEq<&'a T> for Bytes *self == **other } } + +impl Clone for BytesMut { + fn clone(&self) -> BytesMut { + let mut v = Vec::with_capacity(self.len()); + v.extend_from_slice(&self[..]); + BytesMut { + mem: Mem { inner : Arc::new(UnsafeCell::new(v.into_boxed_slice())) }, + pos: self.pos, + len: self.len, + cap: self.cap, + } + } +} diff --git a/tests/test_mut_buf.rs b/tests/test_mut_buf.rs index 796f6a5..97dfbbb 100644 --- a/tests/test_mut_buf.rs +++ b/tests/test_mut_buf.rs @@ -1,8 +1,9 @@ extern crate bytes; extern crate byteorder; -use bytes::BufMut; +use bytes::{Buf, BufMut, ByteBuf}; use std::usize; +use std::fmt::Write; #[test] fn test_vec_as_mut_buf() { @@ -45,3 +46,13 @@ fn test_put_u16() { buf.put_u16::(8532); assert_eq!(b"\x54\x21", &buf[..]); } + +#[test] +fn test_clone() { + let mut buf = ByteBuf::with_capacity(100); + buf.write_str("this is a test").unwrap(); + let buf2 = buf.clone(); + + buf.write_str(" of our emergecy broadcast system").unwrap(); + assert_ne!(buf.bytes(), buf2.bytes()); +}