mirror of
https://github.com/rosenpass/rosenpass.git
synced 2026-02-27 22:13:12 -08:00
feat(API): Use mio::Token based polling
Avoid polling every single IO source to collect events, poll those specific IO sources mio tells us about.
This commit is contained in:
@@ -52,6 +52,56 @@ impl<T, E: TryIoErrorKind> TryIoResultKindHintExt<T> for Result<T, E> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait SubstituteForIoErrorKindExt<T>: Sized {
|
||||
type Error;
|
||||
fn substitute_for_ioerr_kind_with<F: FnOnce() -> T>(
|
||||
self,
|
||||
kind: io::ErrorKind,
|
||||
f: F,
|
||||
) -> Result<T, Self::Error>;
|
||||
fn substitute_for_ioerr_kind(self, kind: io::ErrorKind, v: T) -> Result<T, Self::Error> {
|
||||
self.substitute_for_ioerr_kind_with(kind, || v)
|
||||
}
|
||||
|
||||
fn substitute_for_ioerr_interrupted_with<F: FnOnce() -> T>(
|
||||
self,
|
||||
f: F,
|
||||
) -> Result<T, Self::Error> {
|
||||
self.substitute_for_ioerr_kind_with(io::ErrorKind::Interrupted, f)
|
||||
}
|
||||
|
||||
fn substitute_for_ioerr_interrupted(self, v: T) -> Result<T, Self::Error> {
|
||||
self.substitute_for_ioerr_interrupted_with(|| v)
|
||||
}
|
||||
|
||||
fn substitute_for_ioerr_wouldblock_with<F: FnOnce() -> T>(
|
||||
self,
|
||||
f: F,
|
||||
) -> Result<T, Self::Error> {
|
||||
self.substitute_for_ioerr_kind_with(io::ErrorKind::WouldBlock, f)
|
||||
}
|
||||
|
||||
fn substitute_for_ioerr_wouldblock(self, v: T) -> Result<T, Self::Error> {
|
||||
self.substitute_for_ioerr_wouldblock_with(|| v)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: TryIoErrorKind> SubstituteForIoErrorKindExt<T> for Result<T, E> {
|
||||
type Error = E;
|
||||
|
||||
fn substitute_for_ioerr_kind_with<F: FnOnce() -> T>(
|
||||
self,
|
||||
kind: io::ErrorKind,
|
||||
f: F,
|
||||
) -> Result<T, Self::Error> {
|
||||
match self.try_io_err_kind_hint() {
|
||||
Ok(v) => Ok(v),
|
||||
Err((_, Some(k))) if k == kind => Ok(f()),
|
||||
Err((e, _)) => Err(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Automatically handles `std::io::ErrorKind::Interrupted`.
|
||||
///
|
||||
/// - If there is no error (i.e. on `Ok(r)`), the function will return `Ok(Some(r))`
|
||||
|
||||
Reference in New Issue
Block a user