Bucket

Struct Bucket 

Source
pub struct Bucket { /* private fields */ }
Expand description

This is the main struct to do everything you need

§Global or task-based

Essentially buckets just store usages and limits, meaning you can create a different bucket for each kind of limit: each of your commands, separate buckets for channel and user usage if you want to have different limits for each etc.

§Usage

Register usages using the Bucket::register method after getting the limit with Bucket::limit_duration

IDs use NonZeroU64 to be compatible with any kind of ID: users, guilds or even your custom IDs

Implementations§

Source§

impl Bucket

Source

pub fn new(limit: Limit) -> Self

Create a new Bucket with the given limit

Source

pub fn register(&self, id: u64)

Register a usage, you should call this every time something you want to limit is done after waiting for the limit

if let Some(duration) = bucket.limit_duration(user_id) {
    tokio::time::sleep(duration).await;
}
bucket.register(user_id);
§Panics

If the id is 0 or when the usage count is over u16::MAX

Source

pub fn limit_duration(&self, id: u64) -> Option<Duration>

Get the duration to wait until the next usage by id, returns None if the id isn’t limited, you should call this before registering a usage

if let Some(duration) = bucket.limit_duration(user_id) {
    tokio::time::sleep(duration).await;
}
bucket.register(user_id);
§Panics

If the id is 0

Trait Implementations§

Source§

impl Debug for Bucket

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Bucket

§

impl !RefUnwindSafe for Bucket

§

impl Send for Bucket

§

impl Sync for Bucket

§

impl Unpin for Bucket

§

impl UnwindSafe for Bucket

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.