Skip to content

Commit

Permalink
feat(api): simplify polling
Browse files Browse the repository at this point in the history
  • Loading branch information
hussein-aitlahcen committed Oct 27, 2022
1 parent 2f97690 commit af05ae8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn server(port: u16) {
};

// Process connections events.
let _events_processed = server.poll_event::<100, _>(|event| {
let _events_processed = server.poll_event::<100>(|event| {
match (event.old_state(), event.info().state()) {
// A client is about to connect, accept it.
(
Expand Down Expand Up @@ -141,7 +141,7 @@ fn server(port: u16) {
});

// Process some messages, we arbitrary define 100 as being the max number of messages we can handle per iteration.
let _messages_processed = server.poll_messages::<100, _>(|message| {
let _messages_processed = server.poll_messages::<100>(|message| {
// **unwrap** must be banned in production.
let chat_message = core::str::from_utf8(message.payload()).unwrap();
println!("Boarcasting {}", chat_message);
Expand Down Expand Up @@ -196,7 +196,7 @@ fn client(port: u16) {
client.poll_callbacks();

// Process some messages, we arbitrary define 100 as being the max number of messages we can handle per iteration.
let _messages_processed = client.poll_messages::<100, _>(|message| {
let _messages_processed = client.poll_messages::<100>(|message| {
println!(
"(Chat) {}",
// **unwrap** must be banned in production.
Expand All @@ -206,7 +206,7 @@ fn client(port: u16) {

let mut quit = false;
let _ =
client.poll_event::<100, _>(|event| match (event.old_state(), event.info().state()) {
client.poll_event::<100>(|event| match (event.old_state(), event.info().state()) {
(
ESteamNetworkingConnectionState::k_ESteamNetworkingConnectionState_None,
ESteamNetworkingConnectionState::k_ESteamNetworkingConnectionState_Connecting,
Expand Down
16 changes: 8 additions & 8 deletions gns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,10 +676,10 @@ where
}

#[inline]
pub fn poll_messages<const K: usize, F>(&self, mut message_callback: F) -> Option<usize>
where
F: FnMut(&GnsNetworkMessage<ToReceive>),
{
pub fn poll_messages<const K: usize>(
&self,
mut message_callback: impl FnMut(&GnsNetworkMessage<ToReceive>),
) -> Option<usize> {
// Do not implements default for networking messages as they must be allocated by the lib.
let mut messages: [GnsNetworkMessage<ToReceive>; K] =
unsafe { MaybeUninit::zeroed().assume_init() };
Expand All @@ -695,10 +695,10 @@ where
}

#[inline]
pub fn poll_event<const K: usize, F>(&self, mut event_callback: F) -> usize
where
F: FnMut(GnsConnectionEvent),
{
pub fn poll_event<const K: usize>(
&self,
mut event_callback: impl FnMut(GnsConnectionEvent),
) -> usize {
let mut processed = 0;
'a: while let Some(event) = self.state.queue().pop() {
event_callback(event);
Expand Down

0 comments on commit af05ae8

Please sign in to comment.