From c6df3fd77393c1f32c33db11e397ec2af765f376 Mon Sep 17 00:00:00 2001 From: Jeremy HERGAULT Date: Sun, 24 May 2026 15:54:49 +0200 Subject: [PATCH 1/2] feat: allow capacity allocation for pendings Signed-off-by: Jeremy HERGAULT --- prosa/Cargo.toml | 2 +- prosa/src/event/pending.rs | 45 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) 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..71530a3 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); @@ -466,6 +496,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 mut 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 From b48ec349eac2b4e4ac502aa3fdd641afbc946954 Mon Sep 17 00:00:00 2001 From: Jeremy HERGAULT Date: Sun, 24 May 2026 16:01:59 +0200 Subject: [PATCH 2/2] fix: clippy Signed-off-by: Jeremy HERGAULT --- prosa/src/event/pending.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/prosa/src/event/pending.rs b/prosa/src/event/pending.rs index 71530a3..c34e22a 100644 --- a/prosa/src/event/pending.rs +++ b/prosa/src/event/pending.rs @@ -343,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(); } @@ -505,7 +502,7 @@ mod tests { assert!(pending_msg.is_empty()); assert!(pending_msg.capacity() >= capacity); - let mut pending_timer: Timers = Timers::with_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);