From 41b722dee88efeb8d9a288cdd5ddbff8b641ec75 Mon Sep 17 00:00:00 2001
From: Carl Lerche <me@carllerche.com>
Date: Sat, 23 Jul 2016 09:54:18 -0700
Subject: [PATCH] Allow checking out AppendBufs from Pool

---
 src/alloc/pool.rs   |  7 ++++++-
 test/test_append.rs | 31 +++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/src/alloc/pool.rs b/src/alloc/pool.rs
index 8d746b0..efceb26 100644
--- a/src/alloc/pool.rs
+++ b/src/alloc/pool.rs
@@ -1,5 +1,5 @@
 use super::{Mem, MemRef};
-use buf::{ByteBuf, MutByteBuf};
+use buf::{AppendBuf, ByteBuf, MutByteBuf};
 use stable_heap as heap;
 use std::{mem, ptr, isize, usize};
 use std::cell::{Cell, UnsafeCell};
@@ -92,6 +92,11 @@ impl Pool {
         })
     }
 
+    pub fn new_append_buf(&self) -> Option<AppendBuf> {
+        let len = self.inner.buf_len as u32;
+        self.checkout().map(|mem| unsafe { AppendBuf::from_mem_ref(mem, len, 0) })
+    }
+
     fn checkout(&self) -> Option<MemRef> {
         unsafe {
             let mut ptr = self.inner.next.load(Ordering::Acquire);
diff --git a/test/test_append.rs b/test/test_append.rs
index 227e9ae..1b4db54 100644
--- a/test/test_append.rs
+++ b/test/test_append.rs
@@ -1,4 +1,5 @@
 use bytes::{ByteStr, Buf, MutBuf};
+use bytes::alloc::Pool;
 use bytes::buf::AppendBuf;
 
 #[test]
@@ -28,3 +29,33 @@ pub fn test_initial_buf_empty() {
         assert_eq!(dst, b"hello world");
     }
 }
+
+#[test]
+pub fn test_append_buf_from_pool() {
+    let pool = Pool::with_capacity(2, 256);
+
+    // Run in a loop a bunch in hope that if there is a memory issue, it will
+    // be exposed
+    for _ in 0..1000 {
+        let mut buf = pool.new_append_buf().unwrap();
+        let mut dst: Vec<u8> = vec![];
+
+        assert_eq!(buf.remaining(), 256);
+
+        buf.write_slice(b"hello world");
+        assert_eq!(buf.remaining(), 245);
+        assert_eq!(buf.bytes(), b"hello world");
+
+        let view1 = buf.slice(0, 11);
+        view1.buf().copy_to(&mut dst).unwrap();
+
+        assert_eq!(dst, b"hello world");
+        assert_eq!(view1, buf.slice(0, 11));
+
+        drop(buf);
+        let mut buf = pool.new_append_buf().unwrap();
+        buf.write_slice(b"zomg no no no no");
+
+        assert_eq!(dst, b"hello world");
+    }
+}
-- 
GitLab