pub struct Producer<T> { /* private fields */ }Expand description
A handle to the queue which allows adding values onto the buffer
Implementations§
Source§impl<T> Producer<T>
impl<T> Producer<T>
Sourcepub fn push(&self, v: T)
pub fn push(&self, v: T)
Push a value onto the buffer.
If the buffer is non-full, the operation will execute immediately. If the buffer is full,
this method will block until the buffer is non-full. The waiting strategy is a simple
spin-wait. If you do not want a spin-wait burning CPU, you should call try_push()
directly and implement a different waiting strategy.
§Examples
let (producer, _) = make(100);
// Block until we can push this value onto the queue
producer.push(123);Sourcepub fn try_push(&self, v: T) -> Option<T>
pub fn try_push(&self, v: T) -> Option<T>
Attempt to push a value onto the buffer.
This method does not block. If the queue is not full, the value will be added to the
queue and the method will return None, signifying success. If the queue is full,
this method will return Some(v)``, where v` is your original value.
§Examples
let (producer, _) = make(100);
// Attempt to add this value to the queue
match producer.try push(123) {
Some(v) => {}, // Queue full, try again later
None => {} // Value added to queue
}Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the total capacity of this queue
This value represents the total capacity of the queue when it is full. It does not
represent the current usage. For that, call size().
§Examples
let (producer, _) = make(100);
assert!(producer.capacity() == 100);
producer.push(123);
assert!(producer.capacity() == 100);Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the current size of the queue
This value represents the current size of the queue. This value can be from 0-capacity
inclusive.
§Examples
let (producer, _) = make(100);
assert!(producer.size() == 0);
producer.push(123);
assert!(producer.size() == 1);Sourcepub fn free_space(&self) -> usize
pub fn free_space(&self) -> usize
Returns the available space in the queue
This value represents the number of items that can be pushed onto the queue before it becomes full.
§Examples
let (producer, _) = make(100);
assert!(producer.free_space() == 100);
producer.push(123);
assert!(producer.free_space() == 99);