Skip to main content

BucketBuilder

Struct BucketBuilder 

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

A fluent builder for a Bucket when the Tier-1 constructors are not enough.

Set the capacity (the burst ceiling), the refill rate, and optionally the initial fill, then call build. Anything left unset keeps its default, and build validates the result through BucketConfig::new, so an unworkable combination is rejected rather than producing a misbehaving bucket.

Capacity and burst are the same thing for a token bucket: the bucket holds at most capacity tokens, so the largest single acquire it can ever grant — the burst — is capacity.

For a custom time source, chain Bucket::with_clock onto the built bucket; the builder itself always produces a SystemClock bucket.

§Examples

use better_bucket::Bucket;
use std::time::Duration;

// Burst up to 1000, refill 50/second, start empty.
let bucket = Bucket::builder()
    .capacity(1000)
    .refill(50, Duration::from_secs(1))
    .initial(0)
    .build()?;

assert_eq!(bucket.capacity(), 1000);
assert_eq!(bucket.available(), 0);

Implementations§

Source§

impl BucketBuilder

Source

pub fn new() -> Self

Starts a builder with every field at its default (which build rejects until at least a capacity and refill rate are set).

Source

pub fn capacity(self, capacity: u32) -> Self

Sets the capacity — the maximum tokens the bucket holds, and therefore the largest burst it can grant at once. Required.

§Examples
use better_bucket::Bucket;
use std::time::Duration;

let bucket = Bucket::builder()
    .capacity(200)
    .refill(200, Duration::from_secs(1))
    .build()?;
assert_eq!(bucket.capacity(), 200);
Source

pub fn refill(self, amount: u32, period: Duration) -> Self

Sets the sustained refill rate: amount tokens every period. Required.

§Examples
use better_bucket::Bucket;
use std::time::Duration;

// 10 tokens every 250ms.
let bucket = Bucket::builder()
    .capacity(10)
    .refill(10, Duration::from_millis(250))
    .build()?;
Source

pub fn initial(self, initial: u32) -> Self

Sets the initial number of tokens. Defaults to the capacity (the bucket starts full); values above the capacity are clamped to it.

§Examples
use better_bucket::Bucket;
use std::time::Duration;

let bucket = Bucket::builder()
    .capacity(100)
    .refill(100, Duration::from_secs(1))
    .initial(0) // start empty instead of full
    .build()?;
assert_eq!(bucket.available(), 0);
Source

pub fn build(self) -> Result<Bucket<SystemClock>, BucketError>

Validates the configuration and builds the bucket.

§Errors

Returns a BucketError for the same reasons as BucketConfig::new: zero capacity, zero refill amount, or zero refill period. A freshly created builder fails this way until a capacity and refill rate are set.

§Examples
use better_bucket::{Bucket, BucketError};

// Nothing configured yet → rejected.
let err = Bucket::builder().build().unwrap_err();
assert_eq!(err, BucketError::ZeroCapacity);

Trait Implementations§

Source§

impl Clone for BucketBuilder

Source§

fn clone(&self) -> BucketBuilder

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BucketBuilder

Source§

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

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

impl Default for BucketBuilder

Source§

fn default() -> BucketBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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, 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.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error