remotia_buffer_utils/
lib.rs

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