From ebdfa5fa51255d235ff6299243a324bfb59ef183 Mon Sep 17 00:00:00 2001 From: pjht Date: Wed, 20 Nov 2024 13:58:05 -0600 Subject: [PATCH] Add try_get_list_result to PollEvents --- src/lib.rs | 68 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 97a491a..800c55c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,14 @@ use std::{ - borrow::Cow, boxed::Box, os::mikros::{ipc::{ - self, - rpc::{self, CallId, IncomingCall} - }, Errno}, vec::Vec + borrow::Cow, + boxed::Box, + os::mikros::{ + ipc::{ + self, + rpc::{self, CallId, IncomingCall}, + }, + Errno, + }, + vec::Vec, }; use bitflags::bitflags; @@ -77,7 +83,7 @@ impl PendingPoll { } pub fn try_get_result(&self) -> Option { - let res: PollEvents = postcard::from_bytes(&*self.call_id.try_get_return()?).unwrap(); + let res: PollEvents = postcard::from_bytes(&self.call_id.try_get_return()?).unwrap(); Some(res) } @@ -112,6 +118,37 @@ impl PendingPoll { ipc::process_messages(); } } + + pub fn try_get_list_result(polls: &[Self]) -> Option> { + let mut result_vec = Vec::with_capacity(polls.len()); + let mut poll_returned = false; + let mut first_returned = 0; + for (i, poll) in polls.iter().enumerate() { + if let Some(poll_result) = poll.try_get_result() { + result_vec.push(poll_result); + if !poll_returned { + first_returned = i; + } + poll_returned = true; + } else if poll_returned { + poll.cancel(); + if let Some(poll_result) = poll.try_get_result() { + result_vec.push(poll_result); + } + } + } + if poll_returned { + for poll in &polls[0..first_returned] { + poll.cancel(); + if let Some(poll_result) = poll.try_get_result() { + result_vec.push(poll_result); + } + } + Some(result_vec) + } else { + None + } + } } impl Client { @@ -121,26 +158,16 @@ impl Client { pub fn read(self, fd: u64, len: usize) -> Result, Errno> { postcard::from_bytes( - &rpc::send_call( - self.0, - PROTO, - 0, - &postcard::to_stdvec(&(fd, len)).unwrap(), - ) - .get_return(), + &rpc::send_call(self.0, PROTO, 0, &postcard::to_stdvec(&(fd, len)).unwrap()) + .get_return(), ) .unwrap() } pub fn write(self, fd: u64, data: &[u8]) -> Result<(), Errno> { postcard::from_bytes( - &rpc::send_call( - self.0, - PROTO, - 1, - &postcard::to_stdvec(&(fd, data)).unwrap(), - ) - .get_return(), + &rpc::send_call(self.0, PROTO, 1, &postcard::to_stdvec(&(fd, data)).unwrap()) + .get_return(), ) .unwrap() } @@ -187,7 +214,8 @@ impl Client { pub fn seek(&self, fd: u64, pos: SeekFrom) -> Result { postcard::from_bytes( - &rpc::send_call(self.0, PROTO, 8, &postcard::to_stdvec(&(fd, pos)).unwrap()).get_return(), + &rpc::send_call(self.0, PROTO, 8, &postcard::to_stdvec(&(fd, pos)).unwrap()) + .get_return(), ) .unwrap() }