use std::sync::atomic::{AtomicUsize, Ordering};
use rand::RngExt;
pub type BoxStrategy = Box<dyn Strategy>;
pub trait Strategy: Send + Sync + 'static {
fn next(&self, len: usize) -> usize;
}
pub struct RoundRobin {
counter: AtomicUsize,
}
impl RoundRobin {
#[must_use]
pub fn new() -> Self {
Self {
counter: AtomicUsize::new(0),
}
}
}
impl Default for RoundRobin {
fn default() -> Self {
Self::new()
}
}
impl Strategy for RoundRobin {
fn next(&self, len: usize) -> usize {
self.counter.fetch_add(1, Ordering::Relaxed) % len
}
}
pub struct Random;
impl Strategy for Random {
fn next(&self, len: usize) -> usize {
rand::rng().random_range(0..len)
}
}