Skip to content

Commit 5b029f7

Browse files
committed
refactor: unbounded pool extends one or an iterator
Signed-off-by: tison <wander4096@gmail.com>
1 parent 4f974ff commit 5b029f7

3 files changed

Lines changed: 46 additions & 12 deletions

File tree

examples/buffer/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async fn main() {
4848

4949
async fn manual_put_pool() {
5050
let pool = Pool::<Vec<u8>>::never_manage(PoolConfig::new());
51-
pool.put(Vec::with_capacity(1024));
51+
pool.extend_one(Vec::with_capacity(1024));
5252

5353
let mut buf = pool.get().await.unwrap();
5454
write!(&mut buf, "{:?} key=manual_put_pool_put", Instant::now()).unwrap();
@@ -70,7 +70,7 @@ async fn auto_create_pool() {
7070
write!(&mut buf, "{:?} key=auto_create_pool_0", Instant::now()).unwrap();
7171
println!("{}", String::from_utf8_lossy(&buf));
7272

73-
pool.put(Vec::with_capacity(1024));
73+
pool.extend_one(Vec::with_capacity(1024));
7474
let mut buf = pool.get().await.unwrap();
7575
write!(&mut buf, "{:?} key=auto_create_pool_1", Instant::now()).unwrap();
7676
println!("{}", String::from_utf8_lossy(&buf));

fastpool/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
//! let result = pool.get().await;
100100
//! assert_eq!(result.unwrap_err().to_string(), "unbounded pool is empty");
101101
//!
102-
//! pool.put(Vec::with_capacity(1024));
102+
//! pool.extend_one(Vec::with_capacity(1024));
103103
//! let o = pool.get().await.unwrap();
104104
//! assert_eq!(o.capacity(), 1024);
105105
//! # }

fastpool/src/unbounded.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//! let result = pool.get().await;
3939
//! assert_eq!(result.unwrap_err().to_string(), "unbounded pool is empty");
4040
//!
41-
//! pool.put(Vec::with_capacity(1024));
41+
//! pool.extend_one(Vec::with_capacity(1024));
4242
//! let o = pool.get().await.unwrap();
4343
//! assert_eq!(o.capacity(), 1024);
4444
//! drop(o);
@@ -343,15 +343,49 @@ impl<T, M: ManageObject<Object = T>> Pool<T, M> {
343343
Ok(object)
344344
}
345345

346-
/// Put an object to the pool.
347-
pub fn put(&self, o: T) {
346+
/// Extends the pool with exactly one object.
347+
///
348+
/// # Examples
349+
///
350+
/// ```
351+
/// use fastpool::unbounded::Pool;
352+
/// use fastpool::unbounded::PoolConfig;
353+
///
354+
/// let config = PoolConfig::default();
355+
/// let pool = Pool::never_manage(config);
356+
///
357+
/// pool.extend_one(Vec::with_capacity(1024));
358+
/// ```
359+
pub fn extend_one(&self, o: T) {
360+
self.extend(Some(o));
361+
}
362+
363+
/// Extends the pool with the objects of an iterator.
364+
///
365+
/// # Examples
366+
///
367+
/// ```
368+
/// use fastpool::unbounded::Pool;
369+
/// use fastpool::unbounded::PoolConfig;
370+
///
371+
/// let config = PoolConfig::default();
372+
/// let pool = Pool::never_manage(config);
373+
///
374+
/// pool.extend([
375+
/// Vec::with_capacity(1024),
376+
/// Vec::with_capacity(512),
377+
/// Vec::with_capacity(256),
378+
/// ]);
379+
/// ```
380+
pub fn extend(&self, iter: impl IntoIterator<Item = T>) {
348381
let mut slots = self.slots.lock();
349-
slots.current_size += 1;
350-
slots.deque.push_back(ObjectState {
351-
o,
352-
status: ObjectStatus::default(),
353-
});
354-
drop(slots);
382+
for o in iter {
383+
slots.current_size += 1;
384+
slots.deque.push_back(ObjectState {
385+
o,
386+
status: ObjectStatus::default(),
387+
});
388+
}
355389
}
356390

357391
/// Returns the current status of the pool.

0 commit comments

Comments
 (0)