use std::any::Any;
use rand::seq::SliceRandom as _;
use rand::Rng as _;
use crate::entity::Raw;
pub trait Shard: Send + 'static {
type Raw: Raw;
type Hint: Sized;
fn allocate(&mut self, hint: Self::Hint) -> Self::Raw;
}
pub(crate) trait AnyShard: Send + 'static {
fn as_any_mut(&mut self) -> &mut dyn Any;
fn as_any_box(self: Box<Self>) -> Box<dyn Any>;
}
impl<T: Shard> AnyShard for T {
fn as_any_mut(&mut self) -> &mut dyn Any { self }
fn as_any_box(self: Box<Self>) -> Box<dyn Any> { self }
}
pub trait ShardAssigner: Default + 'static {
fn select_for_offline_allocation(&mut self, num_shards: usize) -> usize;
fn shuffle_shards<T>(&mut self, shards: &mut [T]);
}
#[derive(Default)]
pub struct ThreadRngShardAssigner;
impl ShardAssigner for ThreadRngShardAssigner {
fn select_for_offline_allocation(&mut self, num_shards: usize) -> usize {
rand::thread_rng().gen_range(0..num_shards)
}
fn shuffle_shards<T>(&mut self, shards: &mut [T]) { shards.shuffle(&mut rand::thread_rng()); }
}
#[derive(Debug, Default)]
pub struct StaticShardAssigner {
pub allocating_shard: usize,
}
impl ShardAssigner for StaticShardAssigner {
fn select_for_offline_allocation(&mut self, _num_shards: usize) -> usize {
self.allocating_shard
}
fn shuffle_shards<T>(&mut self, _shards: &mut [T]) {
}
}