Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion prosa/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "prosa"
version = "0.4.3"
version = "0.4.4"
authors.workspace = true
description = "ProSA core"
homepage.workspace = true
Expand Down
52 changes: 47 additions & 5 deletions prosa/src/event/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>) {
let mut timer_iter = self.timers.iter();
Expand Down Expand Up @@ -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>,
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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -466,6 +493,21 @@ mod tests {
}
}

#[test]
fn test_with_capacity() {
let capacity = 10;
let pending_msg: PendingMsgs<RequestMsg<SimpleStringTvf>, 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<u64> = 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
Expand Down
Loading