use alloc::vec::Vec;
use ruda_core::stream_id::StreamId;
pub trait StreamFactory {
type Stream;
fn create(&mut self) -> Self::Stream;
}
#[derive(Debug)]
pub struct StreamPool<F: StreamFactory> {
streams: Vec<Option<F::Stream>>,
factory: F,
max_streams: usize,
}
impl<F: StreamFactory> StreamPool<F> {
pub fn new(backend: F, max_streams: u8, num_special: u8) -> Self {
let mut streams = Vec::with_capacity(max_streams as usize);
for _ in 0..(max_streams.saturating_add(num_special)) {
streams.push(None);
}
Self {
streams,
factory: backend,
max_streams: max_streams as usize,
}
}
pub fn get_mut(&mut self, stream_id: &StreamId) -> &mut F::Stream {
let index = self.stream_index(stream_id);
self.get_mut_index(index)
}
pub fn try_initialize_with<E>(
&mut self, stream_id: &StreamId,
initialize: impl FnOnce(&mut F) -> Result<F::Stream, E>,
) -> Result<bool, E> {
let Ok(index) = usize::try_from(stream_id.value) else { return Ok(false); };
if index >= self.max_streams || self.streams[index].is_some() { return Ok(false); }
let stream = initialize(&mut self.factory)?;
self.streams[index] = Some(stream);
Ok(true)
}
pub fn get_mut_index(&mut self, index: usize) -> &mut F::Stream {
let entry = &mut self.streams[index];
entry.get_or_insert_with(|| self.factory.create())
}
pub fn get_special(&mut self, index: u8) -> &mut F::Stream {
self.get_mut_index(self.max_streams + index as usize)
}
pub fn stream_index(&mut self, id: &StreamId) -> usize {
stream_index(id, self.max_streams)
}
}
pub fn stream_index(stream_id: &StreamId, max_streams: usize) -> usize {
stream_id.value as usize % max_streams
}