diff --git a/prosa/Cargo.toml b/prosa/Cargo.toml index 3249dfe..bfa4f9f 100644 --- a/prosa/Cargo.toml +++ b/prosa/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "prosa" -version = "0.4.3" +version = "0.4.4" authors.workspace = true description = "ProSA core" homepage.workspace = true diff --git a/prosa/src/event/pending.rs b/prosa/src/event/pending.rs index 7e71f74..c34e22a 100644 --- a/prosa/src/event/pending.rs +++ b/prosa/src/event/pending.rs @@ -111,11 +111,23 @@ where self.timers.len() } + /// Returns the capacity of the internal timer list. + pub fn capacity(&self) -> usize { + self.timers.capacity() + } + /// Returns true if there is no pending timer pub fn is_empty(&self) -> bool { self.timers.is_empty() } + /// Method to create a new pending timer with a specific capacity + pub fn with_capacity(capacity: usize) -> Self { + Timers { + timers: Vec::with_capacity(capacity), + } + } + /// Method to push a pending timer fn push_timer(&mut self, timer: PendingTimer) { let mut timer_iter = self.timers.iter(); @@ -259,11 +271,29 @@ where self.pending_messages.len() } + /// Returns the capacity of the internal message map. + pub fn capacity(&self) -> usize { + self.pending_messages.capacity() + } + /// Returns true if there is no pending message pub fn is_empty(&self) -> bool { self.pending_messages.is_empty() } + /// Method to create a new pending message list with a specific capacity + pub fn with_capacity(capacity: usize) -> Self + where + T: Msg, + M: Sized + Clone + Tvf, + { + PendingMsgs { + pending_messages: HashMap::with_capacity(capacity), + timers: Timers::with_capacity(capacity), + phantom: PhantomData, + } + } + /// Method to push a pending message pub fn push(&mut self, msg: T, timeout: Duration) { self.push_with_id(msg.get_id(), msg, timeout); @@ -313,11 +343,8 @@ where timer.sleep().await; } - if let Some(time) = self.timers.pop() { - return self.pull_msg(time.get_timer_id()); - } else { - return None; - } + let time = self.timers.pop()?; + return self.pull_msg(time.get_timer_id()); } else { self.timers.pop(); } @@ -466,6 +493,21 @@ mod tests { } } + #[test] + fn test_with_capacity() { + let capacity = 10; + let pending_msg: PendingMsgs, SimpleStringTvf> = + PendingMsgs::with_capacity(capacity); + assert_eq!(pending_msg.len(), 0); + assert!(pending_msg.is_empty()); + assert!(pending_msg.capacity() >= capacity); + + let pending_timer: Timers = Timers::with_capacity(capacity); + assert_eq!(pending_timer.len(), 0); + assert!(pending_timer.is_empty()); + assert!(pending_timer.capacity() >= capacity); + } + #[tokio::test] async fn test_pending() { /// Dummy settings