Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions examples/buffer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() {

async fn manual_put_pool() {
let pool = Pool::<Vec<u8>>::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();
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion fastpool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//! # }
Expand Down
52 changes: 43 additions & 9 deletions fastpool/src/unbounded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -343,15 +343,49 @@ impl<T, M: ManageObject<Object = T>> Pool<T, M> {
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::<i64>::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::<i64>::with_capacity(1024),
/// Vec::<i64>::with_capacity(512),
/// Vec::<i64>::with_capacity(256),
/// ]);
/// ```
pub fn extend(&self, iter: impl IntoIterator<Item = T>) {
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.
Expand Down