Struct squeue::SyncQueue

source ·
pub struct SyncQueue<T> { /* private fields */ }
Expand description

Thread-safe FIFO queue with a fixed capacity.

Similar to the standard non thread-safe version, but can be used for queueing in multiple consumers/producers contexts. There’s no subscription though, one needs to do some polling.

The peek function needs items to support Clone.

Examples

use squeue::SyncQueue;

// Does not need to be `mut`
let mut queue: SyncQueue<usize> = SyncQueue::new(3);
assert_eq!(None, queue.push(1));
assert_eq!(None, queue.push(2));
assert_eq!(None, queue.push(3));
assert_eq!(Some(1), queue.push(4));
assert_eq!(3, queue.len());

Implementations§

source§

impl<T> SyncQueue<T>

source

pub fn new(capacity: usize) -> Self

Create a new queue.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<String> = SyncQueue::new(10);
assert_eq!(10, queue.capacity());
source

pub fn len(&self) -> usize

Return queue length.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<&str> = SyncQueue::new(10);
assert_eq!(0, queue.len());
queue.push("x");
queue.push("y");
assert_eq!(2, queue.len());
source

pub fn capacity(&self) -> usize

Return queue capacity.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<f64> = SyncQueue::new(100);
assert_eq!(100, queue.capacity());
source

pub fn is_empty(&self) -> bool

Returns true if queue is empty.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<String> = SyncQueue::new(100);
assert!(queue.is_empty());
queue.push(String::from("abc"));
assert!(!queue.is_empty());
source

pub fn is_full(&self) -> bool

Returns true if queue is full.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<usize> = SyncQueue::new(10);
assert!(!queue.is_full());
for i in 0..10 {
    queue.push(i);
}
assert!(queue.is_full());
source

pub fn clear(&self)

Clear all data.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<String> = SyncQueue::new(100);
queue.push(String::from("abc"));
queue.clear();
assert!(queue.is_empty());
source

pub fn resize(&self, capacity: usize) -> usize

Resize queue.

Returns the number of dropped items, if any.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<usize> = SyncQueue::new(10);
assert_eq!(10, queue.capacity());
assert_eq!(0, queue.resize(100));
assert_eq!(100, queue.capacity());
for i in 0..1000 {
    queue.push(i);
}
assert_eq!(100, queue.len());
assert_eq!(90, queue.resize(10));
assert_eq!(10, queue.capacity());
assert_eq!(10, queue.len());
assert_eq!(Some(990), queue.pop());
source

pub fn push(&self, item: T) -> Option<T>

Push data into the queue.

If the queue is full and data needs to be dropped, that data is returned.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<usize> = SyncQueue::new(2);
assert_eq!(None, queue.push(1));
assert_eq!(None, queue.push(10));
assert_eq!(Some(1), queue.push(100));
assert_eq!(Some(10), queue.push(1000));
assert_eq!(2, queue.len());
source

pub fn pop(&self) -> Option<T>

Pop data from the queue.

The oldest item is returned, this works in FIFO mode.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<usize> = SyncQueue::new(5);
assert_eq!(None, queue.pop());
assert_eq!(None, queue.push(1));
assert_eq!(Some(1), queue.pop());
assert_eq!(None, queue.push(2));
assert_eq!(None, queue.push(3));
assert_eq!(Some(2), queue.pop());
assert_eq!(1, queue.len());
source

pub fn drain(&mut self) -> Drain<T>

Creates an iterator which drains the queue.

Use this when you want to get all the items, at a given point, and free space in the queue.

use squeue::SyncQueue;

let mut queue = SyncQueue::new(10);
queue.push(1);
queue.push(10);
let drain = queue.drain();
assert_eq!(0, queue.len());
assert_eq!(2, drain.count());
source§

impl<T> SyncQueue<T>where
T: Clone,

source

pub fn peek(&self) -> Option<T>

Peek data, get it without removing it.

This gives an insight on what pop() would return. Items need to support Clone.

Examples
use squeue::SyncQueue;

let queue: SyncQueue<usize> = SyncQueue::new(5);
assert_eq!(None, queue.peek());
assert_eq!(None, queue.push(1));
assert_eq!(None, queue.push(2));
assert_eq!(Some(1), queue.peek());

Trait Implementations§

source§

impl<T: Clone> Clone for SyncQueue<T>

source§

fn clone(&self) -> SyncQueue<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for SyncQueue<T>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'de, V> Deserialize<'de> for SyncQueue<V>where
V: 'de + Deserialize<'de> + Clone,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,

Deserialize the queue.

Queue and SyncQueue share the same representation so it is fine to (de)serialize from any of them.

use squeue::SyncQueue;

let export = "{\"capacity\":7,\"data\":[1,4,3]}";

let queue: SyncQueue<usize> = serde_json::from_str(&export).unwrap();

assert_eq!(3, queue.len());
assert_eq!(7, queue.capacity());
assert_eq!(Some(3), queue.pop());
assert_eq!(Some(4), queue.pop());
assert_eq!(Some(1), queue.pop());
assert_eq!(None, queue.pop());
source§

impl<T> Display for SyncQueue<T>where
T: Display,

Pretty-print queue content.

Prints the next element to be popped, and the len/capacity.

Examples

use squeue::SyncQueue;

let queue = SyncQueue::new(100);
queue.push(123);
queue.push(4);
queue.push(5);
assert_eq!("[sync] { next: 123, len: 3, capacity: 100 }", format!("{}", queue));
queue.clear();
assert_eq!("[sync] { len: 0, capacity: 100 }", format!("{}", queue));
source§

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

Formats the value using the given formatter. Read more
source§

impl<T> From<Queue<T>> for SyncQueue<T>

Create a thread-safe queue from an ordinary queue.

Examples

use squeue::{Queue, SyncQueue};

let a: Queue<String> = Queue::new(100);
let b = SyncQueue::from(a);
assert_eq!(100, b.capacity());
source§

fn from(queue: Queue<T>) -> SyncQueue<T>

Converts to this type from the input type.
source§

impl<T> From<SyncQueue<T>> for Queue<T>where
T: Clone,

Create an ordinary queue from a thread-safe queue.

This is possibly slow as it is O(n) since it clones the queue.

Examples

use squeue::{Queue, SyncQueue};

let a: SyncQueue<String> = SyncQueue::new(100);
let b = Queue::from(a);
assert_eq!(100, b.capacity());
source§

fn from(queue: SyncQueue<T>) -> Queue<T>

Converts to this type from the input type.
source§

impl<T> FromIterator<T> for SyncQueue<T>

source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a new queue from an iterator.

With this, you can use collect() to build a queue.

Examples
use squeue::SyncQueue;

let src: Vec<usize> = vec![4, 5, 6];

let queue = src.into_iter().collect::<SyncQueue<usize>>();
assert_eq!(Some(4), queue.pop());
assert_eq!(Some(5), queue.pop());
assert_eq!(Some(6), queue.pop());
assert_eq!(None, queue.pop());
assert_eq!(3, queue.capacity());
source§

impl<V> Serialize for SyncQueue<V>where
V: Serialize + Clone,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,

Serialize the sync queue.

This is possibly long, and requires data to support Clone. Internally, extracts non-sync version and serializes it.

use squeue::SyncQueue;
use serde_json::json;

let queue = SyncQueue::new(10);

queue.push(7);
queue.push(22);

let export = json!(&queue).to_string();

assert_eq!("{\"capacity\":10,\"data\":[22,7]}", export);

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for SyncQueue<T>

§

impl<T> Send for SyncQueue<T>where
T: Send + Sync,

§

impl<T> Sync for SyncQueue<T>where
T: Send + Sync,

§

impl<T> Unpin for SyncQueue<T>

§

impl<T> UnwindSafe for SyncQueue<T>

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> ToOwned for Twhere
T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere
T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.
source§

impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,