Skip to content

Commit

Permalink
Refractor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Dec 29, 2022
1 parent f405303 commit 98a72b3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
33 changes: 33 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,3 +652,36 @@ cfg_if! {
}
}
}

#[cfg(any(
target_os = "windows",
target_os = "illumos",
target_os = "solaris",
target_os = "vxworks",
target_os = "fuchsia",
target_os = "horizon",
all(
unix,
not(any(
target_os = "linux",
target_os = "android",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "dragonfly",
))
)
))]
fn unsupported_error(err: impl Into<String>) -> io::Error {
io::Error::new(
#[cfg(not(polling_no_unsupported_error_kind))]
io::ErrorKind::Unsupported,
#[cfg(polling_no_unsupported_error_kind)]
io::ErrorKind::Other,
err.into(),
)
}
38 changes: 12 additions & 26 deletions src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,7 @@ impl Poller {
FdData {
poll_fds_index,
key: ev.key,
remove: match mode {
PollMode::Oneshot => true,
PollMode::Level => false,
PollMode::Edge => {
return Err(io::Error::new(
#[cfg(not(polling_no_unsupported_error_kind))]
io::ErrorKind::Unsupported,
#[cfg(polling_no_unsupported_error_kind)]
io::ErrorKind::Other,
"edge-triggered events are not supported with poll()",
))
}
},
remove: cvt_mode_as_remove(mode)?,
},
);

Expand Down Expand Up @@ -193,19 +181,7 @@ impl Poller {
data.key = ev.key;
let poll_fds_index = data.poll_fds_index;
fds.poll_fds[poll_fds_index].0.events = poll_events(ev);
data.remove = match mode {
PollMode::Oneshot => true,
PollMode::Level => false,
PollMode::Edge => {
return Err(io::Error::new(
#[cfg(not(polling_no_unsupported_error_kind))]
io::ErrorKind::Unsupported,
#[cfg(polling_no_unsupported_error_kind)]
io::ErrorKind::Other,
"edge-triggered events are not supported with poll()",
))
}
};
data.remove = cvt_mode_as_remove(mode)?;

Ok(())
})
Expand Down Expand Up @@ -435,3 +411,13 @@ fn poll(fds: &mut [PollFd], deadline: Option<Instant>) -> io::Result<usize> {
}
}
}

fn cvt_mode_as_remove(mode: PollMode) -> io::Result<bool> {
match mode {
PollMode::Oneshot => Ok(true),
PollMode::Level => Ok(false),
PollMode::Edge => Err(crate::unsupported_error(
"edge-triggered I/O events are not supported in poll()",
)),
}
}
6 changes: 1 addition & 5 deletions src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ impl Poller {
}

if let PollMode::Edge | PollMode::Level = mode {
return Err(io::Error::new(
#[cfg(not(polling_no_unsupported_error_kind))]
io::ErrorKind::Unsupported,
#[cfg(polling_no_unsupported_error_kind)]
io::ErrorKind::Other,
return Err(crate::unsupported_error(
"this kind of event is not supported with event ports",
));
}
Expand Down
14 changes: 3 additions & 11 deletions src/wepoll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ impl Poller {
pub fn new() -> io::Result<Poller> {
let handle = unsafe { we::epoll_create1(0) };
if handle.is_null() {
return Err(io::Error::new(
#[cfg(not(polling_no_unsupported_error_kind))]
io::ErrorKind::Unsupported,
#[cfg(polling_no_unsupported_error_kind)]
io::ErrorKind::Other,
return Err(crate::unsupported_error(
format!(
"Failed to initialize Wepoll: {}\nThis usually only happens for old Windows or Wine.",
io::Error::last_os_error()
Expand Down Expand Up @@ -167,13 +163,9 @@ impl Poller {
PollMode::Level => 0,
PollMode::Oneshot => we::EPOLLONESHOT,
PollMode::Edge => {
return Err(io::Error::new(
#[cfg(not(polling_no_unsupported_error_kind))]
io::ErrorKind::Unsupported,
#[cfg(polling_no_unsupported_error_kind)]
io::ErrorKind::Other,
return Err(crate::unsupported_error(
"edge-triggered events are not supported with wepoll",
))
));
}
};
if ev.readable {
Expand Down

0 comments on commit 98a72b3

Please sign in to comment.