remotia_buffer_utils/
lib.rs1use std::collections::HashMap;
2
3use remotia_core::traits::{FrameProcessor, PullableFrameProperties};
4
5use async_trait::async_trait;
6
7pub mod pool;
8pub mod pool_registry;
9
10pub use bytes::*;
11
12pub type BuffersMap<K> = HashMap<K, BytesMut>;
13
14#[cfg(test)]
15mod tests;
16pub struct BufferAllocator<K> {
17 buffer_key: K,
18 size: usize,
19}
20
21impl<K> BufferAllocator<K> {
22 pub fn new(buffer_key: K, size: usize) -> Self {
23 Self { buffer_key, size }
24 }
25
26 fn allocate_buffer(&self) -> BytesMut {
27 let mut buf = BytesMut::with_capacity(self.size);
28 buf.resize(self.size, 0);
29 buf
30 }
31}
32
33#[async_trait]
34impl<F, K> FrameProcessor<F> for BufferAllocator<K>
35where
36 F: PullableFrameProperties<K, BytesMut> + Send + 'static,
37 K: Copy + Send,
38{
39 async fn process(&mut self, mut frame_data: F) -> Option<F> {
40 frame_data.push(self.buffer_key, self.allocate_buffer());
41 Some(frame_data)
42 }
43}