Struct squeue::Queue

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

FIFO queue with a fixed capacity.

This is in a LRU package because it has a semantic behavior similar to an LRU, in the sense that when capacity is reached, the oldest element is dropped.

In practice it in just some syntaxic sugar over a VecDeque, and data is dropped when you exceed capacity.

Examples

use squeue::Queue;

let mut queue: Queue<usize> = Queue::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> Queue<T>

source

pub fn new(capacity: usize) -> Self

Create a new queue.

Examples
use squeue::Queue;

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

pub fn len(&self) -> usize

Return queue length.

Examples
use squeue::Queue;

let mut queue: Queue<&str> = Queue::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::Queue;

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

pub fn is_empty(&self) -> bool

Returns true if queue is empty.

Examples
use squeue::Queue;

let mut queue: Queue<String> = Queue::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::Queue;

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

pub fn clear(&mut self)

Clear all data.

Examples
use squeue::Queue;

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

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

Resize queue.

Returns the number of dropped items, if any.

Examples
use squeue::Queue;

let mut queue: Queue<usize> = Queue::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(&mut 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::Queue;

let mut queue: Queue<usize> = Queue::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(&mut self) -> Option<T>

Pop data from the queue.

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

Examples
use squeue::Queue;

let mut queue: Queue<usize> = Queue::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 peek(&self) -> Option<&T>

Peek data, get it without removing it.

This gives an insight on what pop() would return.

Examples
use squeue::Queue;

let mut queue: Queue<usize> = Queue::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());
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::Queue;

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

Trait Implementations§

source§

impl<T: Clone> Clone for Queue<T>

source§

fn clone(&self) -> Queue<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 Queue<T>

source§

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

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

impl<'de, T> Deserialize<'de> for Queue<T>where
    T: Deserialize<'de>,

source§

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

Deserialize this value from the given Serde deserializer. Read more
source§

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

Pretty-print queue content.

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

Examples

use squeue::Queue;

let mut queue = Queue::new(100);
queue.push(123);
queue.push(4);
queue.push(5);
assert_eq!("{ next: 123, len: 3, capacity: 100 }", format!("{}", queue));
queue.clear();
assert_eq!("{ 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 Queue<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::Queue;

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

let mut queue = src.into_iter().collect::<Queue<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<T> IntoIterator for Queue<T>

source§

fn into_iter(self) -> IntoIter<T>

Creates an iterator over the queue.

Takes ownership.

Examples
use squeue::Queue;

let mut queue = Queue::new(10);
queue.push(4);
queue.push(5);
queue.push(6);

let sum=queue.into_iter().sum();
assert_eq!(15, sum);
§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T, Global>

Which kind of iterator are we turning this into?
source§

impl<T> PartialEq<Queue<T>> for Queue<T>where
    T: PartialEq,

source§

fn eq(&self, other: &Self) -> bool

Compares two queues. Capacity, content and order must match.

Examples
use squeue::Queue;

let mut queue1 = Queue::new(10);
queue1.push("Alice");
queue1.push("Bob");
let mut queue2 = Queue::new(10);
queue2.push("Oscar");
queue2.push("Alice");
queue2.push("Bob");
assert_ne!(&queue1, &queue2);
queue2.pop();
assert_eq!(&queue1, &queue2);
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Serialize for Queue<T>where
    T: Serialize,

source§

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

Serialize this value into the given Serde serializer. Read more
source§

impl<T> Eq for Queue<T>where
    T: Eq,

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Queue<T>where
    T: RefUnwindSafe,

§

impl<T> Send for Queue<T>where
    T: Send,

§

impl<T> Sync for Queue<T>where
    T: Sync,

§

impl<T> Unpin for Queue<T>where
    T: Unpin,

§

impl<T> UnwindSafe for Queue<T>where
    T: UnwindSafe,

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>,