1 #[cfg(test)] 2 mod chunk_queue_test; 3 4 use super::chunk::*; 5 6 use std::collections::VecDeque; 7 use tokio::sync::RwLock; 8 9 #[derive(Default)] 10 pub(crate) struct ChunkQueue { 11 chunks: RwLock<VecDeque<Box<dyn Chunk + Send + Sync>>>, 12 max_size: usize, // 0 or negative value: unlimited 13 } 14 15 impl ChunkQueue { new(max_size: usize) -> Self16 pub(crate) fn new(max_size: usize) -> Self { 17 ChunkQueue { 18 chunks: RwLock::new(VecDeque::new()), 19 max_size, 20 } 21 } 22 push(&self, c: Box<dyn Chunk + Send + Sync>) -> bool23 pub(crate) async fn push(&self, c: Box<dyn Chunk + Send + Sync>) -> bool { 24 let mut chunks = self.chunks.write().await; 25 26 if self.max_size > 0 && chunks.len() >= self.max_size { 27 false // dropped 28 } else { 29 chunks.push_back(c); 30 true 31 } 32 } 33 pop(&self) -> Option<Box<dyn Chunk + Send + Sync>>34 pub(crate) async fn pop(&self) -> Option<Box<dyn Chunk + Send + Sync>> { 35 let mut chunks = self.chunks.write().await; 36 chunks.pop_front() 37 } 38 peek(&self) -> Option<Box<dyn Chunk + Send + Sync>>39 pub(crate) async fn peek(&self) -> Option<Box<dyn Chunk + Send + Sync>> { 40 let chunks = self.chunks.read().await; 41 chunks.front().map(|chunk| chunk.clone_to()) 42 } 43 } 44