rate_limit_queue

Struct RateLimitQueue

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

A rate limited queue.

Implementations§

Source§

impl<T> RateLimitQueue<T>

Source

pub fn new(quantum: usize, interval: Duration) -> RateLimitQueue<T>

Creates an empty queue.

§Examples
use rate_limit_queue::RateLimitQueue;

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

pub fn with_capacity( cap: usize, quantum: usize, interval: Duration, ) -> RateLimitQueue<T>

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));
Source

pub fn capacity(&mut self) -> usize

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

Source

pub fn reserve_exact(&mut self, additional: usize)

Reserves the minimum capacity for exactly additional more elements.

§Panics

Panics if the new capacity overflows usize.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements.

§Panics

Panics if the new capacity overflows usize.

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the queue as much as possible.

Source

pub fn truncate(&mut self, len: usize)

Shortens the queue, dropping excess elements from the back.

Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

Source

pub fn is_empty(&self) -> bool

Returns true if the queue is empty.

Source

pub fn set_quantum(&mut self, quantum: usize)

Changes the quantum.

Source

pub fn set_interval(&mut self, interval: Duration)

Changes the interval.

Source

pub fn enqueue(&mut self, value: T)

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);
Source

pub fn dequeue(&mut self) -> Option<T>

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));
Source

pub fn try_dequeue(&mut self) -> DequeueResult<T>

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());
Source

pub fn iter(&mut self) -> impl Iterator<Item = &T>

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);
Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>

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§

Source§

impl<T> Extend<T> for RateLimitQueue<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬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§

§

impl<T> Freeze for RateLimitQueue<T>

§

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

§

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

§

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

§

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

§

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

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.