Reexport rustix result, error, asfd

This commit is contained in:
Aode (lion) 2022-02-16 11:32:51 -05:00
parent 7de823580b
commit 493cacd1ec
4 changed files with 26 additions and 28 deletions

View file

@ -1,5 +1,8 @@
use polldance::io::{Nonblocking, ReadBytes};
use polldance::{net::TcpStream, PollManager, Readiness};
use polldance::{
io::{Nonblocking, ReadBytes},
net::TcpStream,
PollManager, Readiness,
};
use std::sync::Arc;
fn main() -> Result<(), Box<dyn std::error::Error>> {

View file

@ -1,9 +1,8 @@
use crate::Readiness;
use rustix::{fd::AsFd, io::PollFd};
use std::num::NonZeroUsize;
use rustix::fd::AsFd;
use rustix::io::PollFd;
use crate::Readiness;
pub use rustix::io::{Error, Result};
pub enum InProgress<T> {
Ready(T),
@ -35,10 +34,7 @@ impl From<ReadBytes> for usize {
}
}
pub fn try_ready<A: AsFd>(
fd: &A,
interest: Readiness,
) -> rustix::io::Result<Nonblocking<Readiness>> {
pub fn try_ready<A: AsFd>(fd: &A, interest: Readiness) -> Result<Nonblocking<Readiness>> {
let mut fds = [PollFd::new(fd, interest.into())];
rustix::io::poll(&mut fds, 0)?;
let revents: Readiness = fds[0].revents().into();
@ -52,15 +48,15 @@ pub fn try_ready<A: AsFd>(
Ok(Nonblocking::Ready(out))
}
pub fn try_read<A: AsFd>(fd: &A, buf: &mut [u8]) -> rustix::io::Result<Nonblocking<ReadBytes>> {
pub fn try_read<A: AsFd>(fd: &A, buf: &mut [u8]) -> Result<Nonblocking<ReadBytes>> {
nonblocking(rustix::io::read(fd, buf).map(ReadBytes::from))
}
pub fn try_write<A: AsFd>(fd: &A, buf: &[u8]) -> rustix::io::Result<Nonblocking<usize>> {
pub fn try_write<A: AsFd>(fd: &A, buf: &[u8]) -> Result<Nonblocking<usize>> {
nonblocking(rustix::io::write(fd, buf))
}
pub fn in_progress<T>(res: rustix::io::Result<T>) -> rustix::io::Result<InProgress<T>> {
pub fn in_progress<T>(res: Result<T>) -> Result<InProgress<T>> {
match res {
Ok(t) => Ok(InProgress::Ready(t)),
Err(e) if e == rustix::io::Error::INPROGRESS => Ok(InProgress::InProgress),
@ -68,7 +64,7 @@ pub fn in_progress<T>(res: rustix::io::Result<T>) -> rustix::io::Result<InProgre
}
}
pub fn nonblocking<T>(res: rustix::io::Result<T>) -> rustix::io::Result<Nonblocking<T>> {
pub fn nonblocking<T>(res: Result<T>) -> Result<Nonblocking<T>> {
match res {
Ok(t) => Ok(Nonblocking::Ready(t)),
Err(e) if e == rustix::io::Error::WOULDBLOCK || e == rustix::io::Error::AGAIN => {

View file

@ -1,7 +1,4 @@
use rustix::{
fd::AsFd,
io::{OwnedFd, PipeFlags, PollFd, PollFlags},
};
use rustix::io::{OwnedFd, PipeFlags, PollFd, PollFlags};
use std::{
borrow::Borrow,
collections::HashMap,
@ -12,6 +9,8 @@ use std::{
pub mod io;
pub mod net;
pub use rustix::fd::AsFd;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct KeyRef(*const ());
@ -58,7 +57,7 @@ pub struct NotifyToken {
free: Arc<OwnedFd>,
}
pub fn notify_pair() -> rustix::io::Result<(Notify, NotifyToken)> {
pub fn notify_pair() -> io::Result<(Notify, NotifyToken)> {
let (registered, free) = rustix::io::pipe_with(PipeFlags::NONBLOCK | PipeFlags::CLOEXEC)?;
Ok((
@ -100,7 +99,7 @@ impl AsFd for Io {
}
impl PollManager {
pub fn new() -> rustix::io::Result<Self> {
pub fn new() -> io::Result<Self> {
let (notify, notify_token) = notify_pair()?;
Ok(Self {
@ -161,7 +160,7 @@ impl PollManager {
self.notify_token.clone()
}
pub fn poll(&mut self, timeout: Option<i32>) -> rustix::io::Result<Vec<(KeyRef, Readiness)>> {
pub fn poll(&mut self, timeout: Option<i32>) -> io::Result<Vec<(KeyRef, Readiness)>> {
let mut poller = Poller::new();
if let Some(timeout) = timeout {
poller.timeout(timeout);
@ -367,7 +366,7 @@ impl<'a> Poller<'a> {
self.timeout = Some(timeout);
}
pub fn poll(mut self) -> rustix::io::Result<Vec<(usize, Readiness)>> {
pub fn poll(mut self) -> io::Result<Vec<(usize, Readiness)>> {
let timeout = self.timeout.unwrap_or(-1);
let n = rustix::io::poll(&mut self.fds, timeout)?;
@ -390,7 +389,7 @@ impl<'a> Poller<'a> {
}
impl NotifyToken {
pub fn notify(self) -> rustix::io::Result<Self> {
pub fn notify(self) -> io::Result<Self> {
if let Err(e) = rustix::io::write(&*self.free, &[0]) {
if e == rustix::io::Error::AGAIN || e == rustix::io::Error::WOULDBLOCK {
return Ok(self);

View file

@ -27,14 +27,14 @@ pub struct TcpListenerBuilder {
}
impl TcpListener {
pub fn try_accept(&self) -> rustix::io::Result<Nonblocking<(TcpStream, SocketAddrAny)>> {
pub fn try_accept(&self) -> crate::io::Result<Nonblocking<(TcpStream, SocketAddrAny)>> {
nonblocking(
rustix::net::acceptfrom_with(&self.inner, AcceptFlags::NONBLOCK | AcceptFlags::CLOEXEC)
.map(|(inner, addr)| (TcpStream { inner }, addr)),
)
}
pub fn bind(ip: IpAddr, port: u16) -> rustix::io::Result<TcpListenerBuilder> {
pub fn bind(ip: IpAddr, port: u16) -> crate::io::Result<TcpListenerBuilder> {
let family = match ip {
IpAddr::V4(_) => AddressFamily::INET,
IpAddr::V6(_) => AddressFamily::INET6,
@ -58,7 +58,7 @@ impl TcpListener {
}
impl TcpStream {
pub fn connect(ip: IpAddr, port: u16) -> rustix::io::Result<TcpStreamBuilder> {
pub fn connect(ip: IpAddr, port: u16) -> crate::io::Result<TcpStreamBuilder> {
let family = match ip {
IpAddr::V4(_) => AddressFamily::INET,
IpAddr::V6(_) => AddressFamily::INET6,
@ -80,7 +80,7 @@ impl TcpStream {
}
impl TcpListenerBuilder {
pub fn try_finish(self) -> rustix::io::Result<Result<TcpListener, TcpListenerBuilder>> {
pub fn try_finish(self) -> crate::io::Result<Result<TcpListener, TcpListenerBuilder>> {
let res = match self.ip {
IpAddr::V4(v4) => {
let sock_addr = SocketAddrV4::new(v4, self.port);
@ -107,7 +107,7 @@ impl TcpListenerBuilder {
}
impl TcpStreamBuilder {
pub fn try_finish(self) -> rustix::io::Result<Result<TcpStream, TcpStreamBuilder>> {
pub fn try_finish(self) -> crate::io::Result<Result<TcpStream, TcpStreamBuilder>> {
let res = match self.ip {
IpAddr::V4(v4) => {
let sock_addr = SocketAddrV4::new(v4, self.port);