Add try_get_list_result to PollEvents

This commit is contained in:
pjht 2024-11-20 13:58:05 -06:00
parent baa5c4d4bf
commit ebdfa5fa51
Signed by: pjht
GPG Key ID: CA239FC6934E6F3A

View File

@ -1,8 +1,14 @@
use std::{ use std::{
borrow::Cow, boxed::Box, os::mikros::{ipc::{ borrow::Cow,
boxed::Box,
os::mikros::{
ipc::{
self, self,
rpc::{self, CallId, IncomingCall} rpc::{self, CallId, IncomingCall},
}, Errno}, vec::Vec },
Errno,
},
vec::Vec,
}; };
use bitflags::bitflags; use bitflags::bitflags;
@ -77,7 +83,7 @@ impl PendingPoll {
} }
pub fn try_get_result(&self) -> Option<PollEvents> { pub fn try_get_result(&self) -> Option<PollEvents> {
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) Some(res)
} }
@ -112,6 +118,37 @@ impl PendingPoll {
ipc::process_messages(); ipc::process_messages();
} }
} }
pub fn try_get_list_result(polls: &[Self]) -> Option<Vec<PollEvents>> {
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 { impl Client {
@ -121,12 +158,7 @@ impl Client {
pub fn read(self, fd: u64, len: usize) -> Result<Vec<u8>, Errno> { pub fn read(self, fd: u64, len: usize) -> Result<Vec<u8>, Errno> {
postcard::from_bytes( postcard::from_bytes(
&rpc::send_call( &rpc::send_call(self.0, PROTO, 0, &postcard::to_stdvec(&(fd, len)).unwrap())
self.0,
PROTO,
0,
&postcard::to_stdvec(&(fd, len)).unwrap(),
)
.get_return(), .get_return(),
) )
.unwrap() .unwrap()
@ -134,12 +166,7 @@ impl Client {
pub fn write(self, fd: u64, data: &[u8]) -> Result<(), Errno> { pub fn write(self, fd: u64, data: &[u8]) -> Result<(), Errno> {
postcard::from_bytes( postcard::from_bytes(
&rpc::send_call( &rpc::send_call(self.0, PROTO, 1, &postcard::to_stdvec(&(fd, data)).unwrap())
self.0,
PROTO,
1,
&postcard::to_stdvec(&(fd, data)).unwrap(),
)
.get_return(), .get_return(),
) )
.unwrap() .unwrap()
@ -187,7 +214,8 @@ impl Client {
pub fn seek(&self, fd: u64, pos: SeekFrom) -> Result<u64, Errno> { pub fn seek(&self, fd: u64, pos: SeekFrom) -> Result<u64, Errno> {
postcard::from_bytes( 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() .unwrap()
} }