use std::rc::Rc;
use std::cell::RefCell;
use std::time::Duration;
pub use self::thread::Thread;
pub use self::process::Process;
pub use self::generic::{Generic, GenericBuilder};
pub mod thread;
pub mod process;
pub mod generic;
pub mod canary;
pub mod counters;
pub mod zero_copy;
use crate::{Bytesable, Push, Pull};
pub trait AllocateBuilder : Send {
type Allocator: Allocate;
fn build(self) -> Self::Allocator;
}
use std::any::Any;
pub trait Exchangeable : Send+Any+Bytesable { }
impl<T: Send+Any+Bytesable> Exchangeable for T { }
pub trait Allocate {
fn index(&self) -> usize;
fn peers(&self) -> usize;
fn allocate<T: Exchangeable>(&mut self, identifier: usize) -> (Vec<Box<dyn Push<T>>>, Box<dyn Pull<T>>);
fn events(&self) -> &Rc<RefCell<Vec<usize>>>;
fn await_events(&self, _duration: Option<Duration>) { }
fn receive(&mut self) { }
fn release(&mut self) { }
fn pipeline<T: 'static>(&mut self, identifier: usize) ->
(thread::ThreadPusher<T>,
thread::ThreadPuller<T>)
{
thread::Thread::new_from(identifier, Rc::clone(self.events()))
}
fn broadcast<T: Exchangeable + Clone>(&mut self, identifier: usize) -> (Box<dyn Push<T>>, Box<dyn Pull<T>>) {
let (pushers, pull) = self.allocate(identifier);
(Box::new(Broadcaster { spare: None, pushers }), pull)
}
}
struct Broadcaster<T> {
spare: Option<T>,
pushers: Vec<Box<dyn Push<T>>>,
}
impl<T: Clone> Push<T> for Broadcaster<T> {
fn push(&mut self, element: &mut Option<T>) {
for pusher in self.pushers.iter_mut().skip(1) {
self.spare.clone_from(element);
pusher.push(&mut self.spare);
}
for pusher in self.pushers.iter_mut().take(1) {
pusher.push(element);
}
}
}
use crate::allocator::zero_copy::bytes_slab::BytesRefill;
pub trait PeerBuilder {
type Peer: AllocateBuilder + Sized;
fn new_vector(peers: usize, refill: BytesRefill) -> Vec<Self::Peer>;
}