BlockingQueue

Struct BlockingQueue 

Source
pub struct BlockingQueue<'a, T> { /* private fields */ }
Expand description

A thread safe blocking queue. Objects are added to the queue via a closure and are owned by the queue as well This means objects in the queue will only be dropped when the queue’s backing buffer is also destroyed.

Implementations§

Source§

impl<'a, T> BlockingQueue<'a, T>

 use rustpool::BlockingQueue;
 use rustpool::RPQueue;

 fn main() {
   let new_value: isize = 1;

    // create a blocking queue with 3 elements set to value 0
    let q = BlockingQueue::<isize>::new(3, || 0);

    println!("simple blocking queue size {}", q.capacity());

    // take the first element
    let v1 = q.take().unwrap();

    println!("first element is {}", v1);

    // remove the remaining items
    let v2 = q.take().unwrap();
    let v3 = q.take().unwrap();

    // how many items available now?
    println!("items in the queue {}", q.available());

    // no more items to return, let's wait at most one second
    let empty = q.take_wait(std::time::Duration::from_secs(1));

    assert_eq!(None, empty);

    // put everything back
    q.offer(v1);
    q.offer(v2);
    q.offer(v3);

    // how many items available now?
    println!("items in the queue {}", q.available());

    // we can't add more items than the original capacity
    q.offer(&new_value);

    // we still have the same number of available items
    println!("items in the queue {}", q.available());
}
Source

pub fn new(size: usize, f: impl FnMut() -> T) -> Self

Create a new queue with fixed size size using custom create function

Examples found in repository?
examples/blocking_queue.rs (line 9)
5fn main() {
6    let new_value: isize = 1;
7
8    // create a blocking queue with 3 elements set to value 0
9    let q = BlockingQueue::<isize>::new(3, || 0);
10
11    println!("simple blocking queue size {}", q.capacity());
12
13    // take the first element
14    let v1 = q.take().unwrap();
15
16    println!("first element is {}", v1);
17
18    // remove the remaining items
19    let v2 = q.take().unwrap();
20    let v3 = q.take().unwrap();
21
22    // how many items available now?
23    println!("items in the queue {}", q.available());
24
25    // no more items to return, let's wait at most one second
26    let empty = q.take_wait(std::time::Duration::from_secs(1));
27
28    assert_eq!(None, empty);
29
30    // put everything back
31    q.offer(v1);
32    q.offer(v2);
33    q.offer(v3);
34
35    // how many items available now?
36    println!("items in the queue {}", q.available());
37
38    // we can't add more items than the original capacity
39    q.offer(&new_value);
40
41    // we still have the same number of available items
42    println!("items in the queue {}", q.available());
43}
Source

pub fn new_arc(size: usize, f: impl FnMut() -> T) -> Arc<Self>

Create a new queue with fixed size wrapped as an atomic reference counter using a custom create function

Trait Implementations§

Source§

impl<'a, T> Drop for BlockingQueue<'a, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T> RPQueue<'a, T> for BlockingQueue<'a, T>

Source§

fn capacity(&self) -> usize

Returns the size of the queue in total number objects originally allocated

Source§

fn available(&self) -> usize

Returns the number of available items in the buffer

Source§

fn take(&self) -> Option<&'a mut T>

Returns an object from the queue or None if the pool is empty. This method allows a race when checking if the pool is empty in order to quickly exit without causing lock contention. This method is preferred over BlockingQueue::take_wait when empty or near empty pools are common.

Source§

fn take_wait(&self, timeout: Duration) -> Option<&'a mut T>

Returns an object from the queue or block until one is available

Source§

fn offer(&self, item: &'a T) -> usize

Returns an object to the pool

Source§

impl<'a, T> Send for BlockingQueue<'a, T>

Source§

impl<'a, T> Sync for BlockingQueue<'a, T>

Auto Trait Implementations§

§

impl<'a, T> !Freeze for BlockingQueue<'a, T>

§

impl<'a, T> RefUnwindSafe for BlockingQueue<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Unpin for BlockingQueue<'a, T>

§

impl<'a, T> UnwindSafe for BlockingQueue<'a, T>
where T: RefUnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.