pub struct RateLimitQueue<T> { /* private fields */ }
Expand description

A rate limited queue.

Implementations

Creates an empty queue.

Examples
use rate_limit_queue::RateLimitQueue;

let queue: RateLimitQueue<i32> = RateLimitQueue::new(100, Duration::from_secs(1));

Creates an empty queue with space for at least n elements.

Examples
use rate_limit_queue::RateLimitQueue;

let queue: RateLimitQueue<u32> = RateLimitQueue::with_capacity(10, 100, Duration::from_secs(1));

Returns the number of elements the queue can hold without reallocating.

Reserves the minimum capacity for exactly additional more elements.

Panics

Panics if the new capacity overflows usize.

Reserves capacity for at least additional more elements.

Panics

Panics if the new capacity overflows usize.

Shrinks the capacity of the queue as much as possible.

Shortens the queue, dropping excess elements from the back.

Returns the number of elements in the queue.

Returns true if the queue is empty.

Changes the quantum.

Changes the interval.

Appends an element to the back of the queue.

Examples
use rate_limit_queue::RateLimitQueue;

let mut queue = RateLimitQueue::new(100, Duration::from_secs(1));
queue.enqueue(1);
queue.enqueue(2);

Removes the first element and returns it, or None if the queue is empty.

Sleeps if the limit has been reached.

Examples
use rate_limit_queue::RateLimitQueue;

let mut queue = RateLimitQueue::new(100, Duration::from_secs(1));
queue.enqueue(1);
queue.enqueue(2);

assert_eq!(queue.dequeue(), Some(1));
assert_eq!(queue.dequeue(), Some(2));

Tries to remove the first element and return it.

Examples
use rate_limit_queue::{RateLimitQueue, DequeueResult};

let mut queue = RateLimitQueue::new(2, Duration::from_secs(10));
queue.enqueue(1);
queue.enqueue(2);

assert_eq!(queue.try_dequeue(), DequeueResult::Data(1));
assert_eq!(queue.try_dequeue(), DequeueResult::Data(2));
assert_eq!(queue.try_dequeue(), DequeueResult::Empty);

queue.enqueue(3);
assert!(queue.try_dequeue().is_limit());

Returns a front-to-back iterator.

Examples
use rate_limit_queue::{RateLimitQueue, DequeueResult};

let mut queue = RateLimitQueue::new(2, Duration::from_secs(10));
queue.enqueue(1);
queue.enqueue(2);

let b: &[_] = &[&1, &2];
let c: Vec<&i32> = queue.iter().collect();
assert_eq!(&c[..], b);

Returns a front-to-back iterator that returns mutable references.

Examples
use rate_limit_queue::{RateLimitQueue, DequeueResult};

let mut queue = RateLimitQueue::new(2, Duration::from_secs(10));
queue.enqueue(1);
queue.enqueue(2);

let b: &[_] = &[&mut 1, &mut 2];
let c: Vec<&mut i32> = queue.iter_mut().collect();
assert_eq!(&c[..], b);

Trait Implementations

Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.