Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,32 @@
//! # use std::io::Read;
//! # let mut some_file = std::io::empty();
//! let pool: Pool<Vec<u8>> = Pool::new(32, || Vec::with_capacity(4096));
//! let mut reusable_buff = pool.try_pull().unwrap(); // returns None when the pool is saturated
//! reusable_buff.clear(); // clear the buff before using
//! some_file.read_to_end(&mut reusable_buff).ok();
//! // reusable_buff is automatically returned to the pool when it goes out of scope
//!
//! {
//! let mut reusable_buff = pool.try_pull().unwrap(); // returns None when the pool is saturated
//! reusable_buff.clear(); // clear the buff before using
//! some_file.read_to_end(&mut reusable_buff).ok();
//! // reusable_buff is automatically returned to the pool when it goes out of scope
//! }
//!
//! assert_eq!(pool.into_iter().count(), 32);
//! ```
//! Pull from pool and `detach()`
//! ```
//! # use object_pool::Pool;
//! let pool: Pool<Vec<u8>> = Pool::new(32, || Vec::with_capacity(4096));
//! let mut reusable_buff = pool.try_pull().unwrap(); // returns None when the pool is saturated
//! reusable_buff.clear(); // clear the buff before using
//! let (pool, reusable_buff) = reusable_buff.detach();
//! let mut s = String::from_utf8(reusable_buff).unwrap();
//! s.push_str("hello, world!");
//! pool.attach(s.into_bytes()); // reattach the buffer before reusable goes out of scope
//! // reusable_buff is automatically returned to the pool when it goes out of scope
//!
//! {
//! let (pool, reusable_buff) = reusable_buff.detach();
//! let mut s = String::from_utf8(reusable_buff).unwrap();
//! s.push_str("hello, world!");
//! pool.attach(s.into_bytes()); // reattach the buffer before reusable goes out of scope
//! // reusable_buff is automatically returned to the pool when it goes out of scope
//! }
//!
//! assert_eq!(pool.into_iter().count(), 32);
//! ```
//!
//! ## Using Across Threads
Expand Down Expand Up @@ -172,6 +182,15 @@ impl<T> FromIterator<T> for Pool<T> {
}
}

impl<T> IntoIterator for Pool<T> {
type Item = T;
type IntoIter = <Vec<T> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.objects.into_inner().into_iter()
}
}

pub struct Reusable<'a, T> {
pool: &'a Pool<T>,
data: ManuallyDrop<T>,
Expand Down
Loading