picodata_rust/
strategy.rs

1use std::sync::atomic::{AtomicUsize, Ordering};
2
3use rand::Rng;
4
5/// Boxed strategy alias.
6pub type BoxStrategy = Box<dyn Strategy>;
7
8/// Wildcard strategy definition.
9pub trait Strategy: Send + Sync + 'static {
10    fn next(&self, len: usize) -> usize;
11}
12
13/// Returns next index element within it's collection len.
14pub 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
39/// Returns random index element within it's collection len.
40pub struct Random;
41
42impl Strategy for Random {
43    fn next(&self, len: usize) -> usize {
44        rand::rng().random_range(0..len)
45    }
46}