diff --git a/examples/buffer/src/main.rs b/examples/buffer/src/main.rs index a1df2e5..d1aa4f8 100644 --- a/examples/buffer/src/main.rs +++ b/examples/buffer/src/main.rs @@ -48,7 +48,7 @@ async fn main() { async fn manual_put_pool() { let pool = Pool::>::never_manage(PoolConfig::new()); - pool.put(Vec::with_capacity(1024)); + pool.extend_one(Vec::with_capacity(1024)); let mut buf = pool.get().await.unwrap(); write!(&mut buf, "{:?} key=manual_put_pool_put", Instant::now()).unwrap(); @@ -70,7 +70,7 @@ async fn auto_create_pool() { write!(&mut buf, "{:?} key=auto_create_pool_0", Instant::now()).unwrap(); println!("{}", String::from_utf8_lossy(&buf)); - pool.put(Vec::with_capacity(1024)); + pool.extend_one(Vec::with_capacity(1024)); let mut buf = pool.get().await.unwrap(); write!(&mut buf, "{:?} key=auto_create_pool_1", Instant::now()).unwrap(); println!("{}", String::from_utf8_lossy(&buf)); diff --git a/fastpool/src/lib.rs b/fastpool/src/lib.rs index 2c3ae97..bb5bea8 100644 --- a/fastpool/src/lib.rs +++ b/fastpool/src/lib.rs @@ -99,7 +99,7 @@ //! let result = pool.get().await; //! assert_eq!(result.unwrap_err().to_string(), "unbounded pool is empty"); //! -//! pool.put(Vec::with_capacity(1024)); +//! pool.extend_one(Vec::with_capacity(1024)); //! let o = pool.get().await.unwrap(); //! assert_eq!(o.capacity(), 1024); //! # } diff --git a/fastpool/src/unbounded.rs b/fastpool/src/unbounded.rs index b5326fb..eb9f807 100644 --- a/fastpool/src/unbounded.rs +++ b/fastpool/src/unbounded.rs @@ -38,7 +38,7 @@ //! let result = pool.get().await; //! assert_eq!(result.unwrap_err().to_string(), "unbounded pool is empty"); //! -//! pool.put(Vec::with_capacity(1024)); +//! pool.extend_one(Vec::with_capacity(1024)); //! let o = pool.get().await.unwrap(); //! assert_eq!(o.capacity(), 1024); //! drop(o); @@ -343,15 +343,49 @@ impl> Pool { Ok(object) } - /// Put an object to the pool. - pub fn put(&self, o: T) { + /// Extends the pool with exactly one object. + /// + /// # Examples + /// + /// ``` + /// use fastpool::unbounded::Pool; + /// use fastpool::unbounded::PoolConfig; + /// + /// let config = PoolConfig::default(); + /// let pool = Pool::never_manage(config); + /// + /// pool.extend_one(Vec::::with_capacity(1024)); + /// ``` + pub fn extend_one(&self, o: T) { + self.extend(Some(o)); + } + + /// Extends the pool with the objects of an iterator. + /// + /// # Examples + /// + /// ``` + /// use fastpool::unbounded::Pool; + /// use fastpool::unbounded::PoolConfig; + /// + /// let config = PoolConfig::default(); + /// let pool = Pool::never_manage(config); + /// + /// pool.extend([ + /// Vec::::with_capacity(1024), + /// Vec::::with_capacity(512), + /// Vec::::with_capacity(256), + /// ]); + /// ``` + pub fn extend(&self, iter: impl IntoIterator) { let mut slots = self.slots.lock(); - slots.current_size += 1; - slots.deque.push_back(ObjectState { - o, - status: ObjectStatus::default(), - }); - drop(slots); + for o in iter { + slots.current_size += 1; + slots.deque.push_back(ObjectState { + o, + status: ObjectStatus::default(), + }); + } } /// Returns the current status of the pool.