use async_std::sync::Arc;
use netmod::Endpoint;
use std::sync::atomic::{AtomicUsize, Ordering};
type Ep = dyn Endpoint + 'static + Send + Sync;
type EpVec = Vec<Box<Ep>>;
#[derive(Default)]
pub(crate) struct DriverMap {
curr: AtomicUsize,
map: EpVec,
}
impl DriverMap {
pub(crate) fn new() -> Arc<Self> {
Arc::new(Self::default())
}
pub(crate) fn len(&self) -> usize {
self.curr.load(Ordering::Relaxed)
}
#[allow(mutable_transmutes)]
pub(crate) unsafe fn add<E>(&self, ep: E)
where
E: Endpoint + 'static + Send + Sync,
{
let map: &mut EpVec = std::mem::transmute(&self.map);
let curr = self.curr.fetch_add(1, Ordering::Relaxed);
map[curr + 1] = Box::new(ep);
}
#[allow(mutable_transmutes)]
pub(crate) unsafe fn get_mut(&self, id: usize) -> &mut Ep {
let map: &mut EpVec = std::mem::transmute(&self.map);
&mut *map[id]
}
}