diff --git a/mofa-dora-bridge/src/shared_state.rs b/mofa-dora-bridge/src/shared_state.rs index d62670a9..fdaa2edd 100644 --- a/mofa-dora-bridge/src/shared_state.rs +++ b/mofa-dora-bridge/src/shared_state.rs @@ -97,7 +97,7 @@ use crate::data::{AudioData, ChatMessage, LogEntry}; /// assert_eq!(vec.read_all(), vec![2, 3, 4]); /// ``` pub struct DirtyVec { - data: RwLock>, + data: RwLock>, dirty: AtomicBool, max_size: usize, } @@ -105,7 +105,7 @@ pub struct DirtyVec { impl DirtyVec { pub fn new(max_size: usize) -> Self { Self { - data: RwLock::new(Vec::new()), + data: RwLock::new(VecDeque::new()), dirty: AtomicBool::new(false), max_size, } @@ -114,9 +114,9 @@ impl DirtyVec { /// Push item, mark dirty, enforce max size pub fn push(&self, item: T) { let mut data = self.data.write(); - data.push(item); + data.push_back(item); if data.len() > self.max_size { - data.remove(0); + data.pop_front(); } self.dirty.store(true, Ordering::Release); } @@ -124,7 +124,7 @@ impl DirtyVec { /// Read all data if dirty, clearing dirty flag pub fn read_if_dirty(&self) -> Option> { if self.dirty.swap(false, Ordering::AcqRel) { - Some(self.data.read().clone()) + Some(self.data.read().iter().cloned().collect()) } else { None } @@ -132,7 +132,7 @@ impl DirtyVec { /// Read all data unconditionally pub fn read_all(&self) -> Vec { - self.data.read().clone() + self.data.read().iter().cloned().collect() } /// Clear all data @@ -243,7 +243,7 @@ impl Default for DirtyValue { /// Messages without `session_id` are never consolidated, even from the same sender. /// This is a safety feature to prevent accidental merging. pub struct ChatState { - messages: RwLock>, + messages: RwLock>, dirty: AtomicBool, max_messages: usize, } @@ -251,7 +251,7 @@ pub struct ChatState { impl ChatState { pub fn new(max_messages: usize) -> Self { Self { - messages: RwLock::new(Vec::new()), + messages: RwLock::new(VecDeque::new()), dirty: AtomicBool::new(false), max_messages, } @@ -284,11 +284,11 @@ impl ChatState { } } else { // New message - messages.push(msg); + messages.push_back(msg); // Enforce max size if messages.len() > self.max_messages { - messages.remove(0); + messages.pop_front(); } } @@ -298,7 +298,7 @@ impl ChatState { /// Read all messages if dirty pub fn read_if_dirty(&self) -> Option> { if self.dirty.swap(false, Ordering::AcqRel) { - Some(self.messages.read().clone()) + Some(self.messages.read().iter().cloned().collect()) } else { None } @@ -306,7 +306,7 @@ impl ChatState { /// Read all messages unconditionally pub fn read_all(&self) -> Vec { - self.messages.read().clone() + self.messages.read().iter().cloned().collect() } /// Clear all messages