use std::fmt;
use miow::iocp::CompletionStatus;
use super::afd;
use crate::Token;
pub struct Event {
pub flags: u32,
pub data: u64,
}
pub fn token(event: &Event) -> Token {
Token(event.data as usize)
}
pub(crate) const READABLE_FLAGS: u32 = afd::POLL_RECEIVE
| afd::POLL_DISCONNECT
| afd::POLL_ACCEPT
| afd::POLL_ABORT
| afd::POLL_CONNECT_FAIL;
pub(crate) const WRITABLE_FLAGS: u32 = afd::POLL_SEND | afd::POLL_ABORT | afd::POLL_CONNECT_FAIL;
pub(crate) const ERROR_FLAGS: u32 = afd::POLL_CONNECT_FAIL;
pub(crate) const READ_CLOSED_FLAGS: u32 =
afd::POLL_DISCONNECT | afd::POLL_ABORT | afd::POLL_CONNECT_FAIL;
pub(crate) const WRITE_CLOSED_FLAGS: u32 = afd::POLL_ABORT | afd::POLL_CONNECT_FAIL;
pub fn is_readable(event: &Event) -> bool {
event.flags & READABLE_FLAGS != 0
}
pub fn is_writable(event: &Event) -> bool {
event.flags & WRITABLE_FLAGS != 0
}
pub fn is_error(event: &Event) -> bool {
event.flags & ERROR_FLAGS != 0
}
pub fn is_read_closed(event: &Event) -> bool {
event.flags & READ_CLOSED_FLAGS != 0
}
pub fn is_write_closed(event: &Event) -> bool {
event.flags & WRITE_CLOSED_FLAGS != 0
}
pub fn is_priority(event: &Event) -> bool {
event.flags & afd::POLL_RECEIVE_EXPEDITED != 0
}
pub fn is_aio(_: &Event) -> bool {
false
}
pub fn is_lio(_: &Event) -> bool {
false
}
pub fn debug_details(f: &mut fmt::Formatter<'_>, event: &Event) -> fmt::Result {
#[allow(clippy::trivially_copy_pass_by_ref)]
fn check_flags(got: &u32, want: &u32) -> bool {
(got & want) != 0
}
debug_detail!(
FlagsDetails(u32),
check_flags,
afd::POLL_RECEIVE,
afd::POLL_RECEIVE_EXPEDITED,
afd::POLL_SEND,
afd::POLL_DISCONNECT,
afd::POLL_ABORT,
afd::POLL_LOCAL_CLOSE,
afd::POLL_CONNECT,
afd::POLL_ACCEPT,
afd::POLL_CONNECT_FAIL,
);
f.debug_struct("event")
.field("flags", &FlagsDetails(event.flags))
.field("data", &event.data)
.finish()
}
pub struct Events {
pub statuses: Box<[CompletionStatus]>,
pub events: Vec<Event>,
}
impl Events {
pub fn with_capacity(cap: usize) -> Events {
Events {
statuses: vec![CompletionStatus::zero(); cap].into_boxed_slice(),
events: Vec::with_capacity(cap),
}
}
pub fn is_empty(&self) -> bool {
self.events.is_empty()
}
pub fn capacity(&self) -> usize {
self.events.capacity()
}
pub fn len(&self) -> usize {
self.events.len()
}
pub fn get(&self, idx: usize) -> Option<&Event> {
self.events.get(idx)
}
pub fn clear(&mut self) {
self.events.clear();
for status in self.statuses.iter_mut() {
*status = CompletionStatus::zero();
}
}
}