Skip to content

Commit 3c2db58

Browse files
authored
feat: allow capacity allocation for pendings (#75)
Signed-off-by: Jeremy HERGAULT <jeremy.hergault@worldline.com>
1 parent 7fa5e00 commit 3c2db58

2 files changed

Lines changed: 48 additions & 6 deletions

File tree

prosa/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "prosa"
3-
version = "0.4.3"
3+
version = "0.4.4"
44
authors.workspace = true
55
description = "ProSA core"
66
homepage.workspace = true

prosa/src/event/pending.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,23 @@ where
111111
self.timers.len()
112112
}
113113

114+
/// Returns the capacity of the internal timer list.
115+
pub fn capacity(&self) -> usize {
116+
self.timers.capacity()
117+
}
118+
114119
/// Returns true if there is no pending timer
115120
pub fn is_empty(&self) -> bool {
116121
self.timers.is_empty()
117122
}
118123

124+
/// Method to create a new pending timer with a specific capacity
125+
pub fn with_capacity(capacity: usize) -> Self {
126+
Timers {
127+
timers: Vec::with_capacity(capacity),
128+
}
129+
}
130+
119131
/// Method to push a pending timer
120132
fn push_timer(&mut self, timer: PendingTimer<T>) {
121133
let mut timer_iter = self.timers.iter();
@@ -259,11 +271,29 @@ where
259271
self.pending_messages.len()
260272
}
261273

274+
/// Returns the capacity of the internal message map.
275+
pub fn capacity(&self) -> usize {
276+
self.pending_messages.capacity()
277+
}
278+
262279
/// Returns true if there is no pending message
263280
pub fn is_empty(&self) -> bool {
264281
self.pending_messages.is_empty()
265282
}
266283

284+
/// Method to create a new pending message list with a specific capacity
285+
pub fn with_capacity(capacity: usize) -> Self
286+
where
287+
T: Msg<M>,
288+
M: Sized + Clone + Tvf,
289+
{
290+
PendingMsgs {
291+
pending_messages: HashMap::with_capacity(capacity),
292+
timers: Timers::with_capacity(capacity),
293+
phantom: PhantomData,
294+
}
295+
}
296+
267297
/// Method to push a pending message
268298
pub fn push(&mut self, msg: T, timeout: Duration) {
269299
self.push_with_id(msg.get_id(), msg, timeout);
@@ -313,11 +343,8 @@ where
313343
timer.sleep().await;
314344
}
315345

316-
if let Some(time) = self.timers.pop() {
317-
return self.pull_msg(time.get_timer_id());
318-
} else {
319-
return None;
320-
}
346+
let time = self.timers.pop()?;
347+
return self.pull_msg(time.get_timer_id());
321348
} else {
322349
self.timers.pop();
323350
}
@@ -466,6 +493,21 @@ mod tests {
466493
}
467494
}
468495

496+
#[test]
497+
fn test_with_capacity() {
498+
let capacity = 10;
499+
let pending_msg: PendingMsgs<RequestMsg<SimpleStringTvf>, SimpleStringTvf> =
500+
PendingMsgs::with_capacity(capacity);
501+
assert_eq!(pending_msg.len(), 0);
502+
assert!(pending_msg.is_empty());
503+
assert!(pending_msg.capacity() >= capacity);
504+
505+
let pending_timer: Timers<u64> = Timers::with_capacity(capacity);
506+
assert_eq!(pending_timer.len(), 0);
507+
assert!(pending_timer.is_empty());
508+
assert!(pending_timer.capacity() >= capacity);
509+
}
510+
469511
#[tokio::test]
470512
async fn test_pending() {
471513
/// Dummy settings

0 commit comments

Comments
 (0)