picodata_rust/
strategy.rs1use std::sync::atomic::{AtomicUsize, Ordering};
2
3use rand::Rng;
4
5pub type BoxStrategy = Box<dyn Strategy>;
7
8pub trait Strategy: Send + Sync + 'static {
10 fn next(&self, len: usize) -> usize;
11}
12
13pub struct RoundRobin {
15 counter: AtomicUsize,
16}
17
18impl RoundRobin {
19 #[must_use]
20 pub fn new() -> Self {
21 Self {
22 counter: AtomicUsize::new(0),
23 }
24 }
25}
26
27impl Default for RoundRobin {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl Strategy for RoundRobin {
34 fn next(&self, len: usize) -> usize {
35 self.counter.fetch_add(1, Ordering::Relaxed) % len
36 }
37}
38
39pub struct Random;
41
42impl Strategy for Random {
43 fn next(&self, len: usize) -> usize {
44 rand::rng().random_range(0..len)
45 }
46}