pub struct PriorityPool<T, P>where
    P: Ord,{ /* private fields */ }
Expand description

A pool that can be used to borrow items with a priority.

This internally uses a BinaryHeap which is a max-heap. This means that the item with the highest priority will be returned first.

Type parameters

  • T: The type of the items.
  • P: The type of the priority.

Examples

Non-async example:

use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
pool.put("world", 2); // Larger value means higher priority.
 
// Get the highest priority item.
let item = pool.try_get();
assert!(item.is_some());
let item = item.unwrap();
assert_eq!(item.item(), &"world");
 
let another_item = pool.try_get();
assert!(another_item.is_some());
let another_item = another_item.unwrap();
assert_eq!(another_item.item(), &"hello");
 
// The pool is now empty.
let empty_item = pool.try_get();
assert!(empty_item.is_none());
 
drop(another_item); // Return the item back to the pool by dropping it.
let hello = pool.try_get();
assert!(hello.is_some());
assert_eq!(hello.unwrap().item(), &"hello");

Async example:

use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
pool.put("world", 2); // Larger value means higher priority.
 
futures::executor::block_on(async {
    // Get the highest priority item.
    let world = pool.get().await;
    assert_eq!(world.item(), &"world"); 

    let hello = pool.get().await;
    assert_eq!(hello.item(), &"hello");
 
    // The pool is now empty.
    drop(hello); // Return the item back to the pool by dropping it.
    let item = pool.get().await;
    assert_eq!(item.item(), &"hello"); 
});

Implementations§

source§

impl<T, P> PriorityPool<T, P>where P: Ord,

source

pub fn new() -> Self

Creates a new PriorityPool.

source

pub fn with_capacity(capacity: usize) -> Self

Creates a new PriorityPool with the given capacity that is used for both the ready and borrowed queues.

source

pub fn len(&self) -> usize

Returns the number of items in the pool.

source

pub fn is_empty(&self) -> bool

Returns true if the pool is empty.

source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the pool as much as possible.

source

pub fn put(&mut self, item: T, priority: P)

Puts an item T with the given priority P into the pool.

Example
use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
source

pub fn try_get(&mut self) -> Option<PooledItem<T, P>>

Tries to get the highest priority item that is immediately available from the PriorityPool. Returns None if the pool is empty.

Please note that if the sender is dropped before returning the item to the pool, the item will be lost and this will be reflected in the pool’s length.

Example
use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
let item = pool.try_get();
assert!(item.is_some());
source

pub fn blocking_get(&mut self) -> PooledItem<T, P>

Gets the highest priority item that is available from the PriorityPool. If there is no item immediately available, this will wait in a blocking manner until an item is available.

Example
use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
let item = pool.blocking_get();
Panic

This will panic if the sender is dropped before returning the item to the pool, which should never happen.

source

pub fn get(&mut self) -> Get<'_, T, P>

Gets the highest priority item that is available from the PriorityPool. If there is no item immediately available, this will .await until one becomes available.

Example
use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
futures::executor::block_on(async {
   let item = pool.get().await;
});
Panic

This will panic if the sender is dropped before returning the item to the pool, which should never happen.

source

pub fn try_pop(&mut self) -> Option<(T, P)>

Tries to remove the highest priority item that is immediately available from the PriorityPool. Returns None if the pool is empty.

Please note that if the sender is dropped before returning the item to the pool, the item will be lost and this will be reflected in the pool’s length.

Example
use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
assert_eq!(pool.len(), 1);
 
let item = pool.try_pop();
assert!(item.is_some());
assert_eq!(pool.len(), 0);
source

pub fn blocking_pop(&mut self) -> (T, P)

Removes the highest priority item that is available from the PriorityPool. If there is no item immediately available, this will wait in a blocking manner until an item is available.

Example
use piscina::PriorityPool;
 
let mut pool = PriorityPool::new();
pool.put("hello", 1);
assert_eq!(pool.len(), 1);
 
let item = pool.blocking_pop();
assert_eq!(pool.len(), 0);
Panic

This will panic if the sender is dropped before returning the item to the pool, which should never happen.

source

pub fn pop(&mut self) -> Pop<'_, T, P>

Removes the highest priority item that is available from the PriorityPool. If there is no item immediately available, this will .await until one becomes available.

Example
use piscina::PriorityPool;
   
let mut pool = PriorityPool::new();
pool.put("hello", 1);
assert_eq!(pool.len(), 1);
 
futures::executor::block_on(async {
   let item = pool.pop().await;
   assert_eq!(pool.len(), 0);
});

Trait Implementations§

source§

impl<T: Debug, P> Debug for PriorityPool<T, P>where P: Ord + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, P> !RefUnwindSafe for PriorityPool<T, P>

§

impl<T, P> Send for PriorityPool<T, P>where P: Send, T: Send,

§

impl<T, P> Sync for PriorityPool<T, P>where P: Send + Sync, T: Send + Sync,

§

impl<T, P> Unpin for PriorityPool<T, P>where P: Unpin, T: Unpin,

§

impl<T, P> !UnwindSafe for PriorityPool<T, P>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.