Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recv_many function #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ where
#[async_trait::async_trait]
pub trait RecvAdapter<P>: Send + Sync {
async fn recv(&mut self) -> Result<Message<P>, Error>;
async fn recv_many(&mut self, limit: usize) -> Result<Vec<Message<P>>, Error>;
}

pub trait RecvPort<A, P>
Expand Down Expand Up @@ -188,6 +189,16 @@ where

Ok(msg)
}

pub async fn recv_many(&mut self, limit: usize) -> Result<Vec<Message<P>>, Error> {
let receiver = self.receiver.as_mut().ok_or(Error::NotConnected)?;

let msgs = receiver.recv_many(limit).await?;

self.counter += msgs.len() as u64;

Ok(msgs)
}
}

impl<A, P> RecvPort<A, P> for InputPort<A, P>
Expand Down Expand Up @@ -310,6 +321,14 @@ pub mod tokio {
None => Err(Error::RecvError),
}
}

async fn recv_many(&mut self, limit: usize) -> Result<Vec<Message<P>>, Error> {
let mut buffer = Vec::with_capacity(limit);

self.0.recv_many(&mut buffer, limit).await;

Ok(buffer)
}
}

pub type OutputPort<P> = super::OutputPort<ChannelSendAdapter<P>, P>;
Expand Down