use super::{IfReq, Opener, Queue, Result};
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
use async_io::Async;
use futures_io::{AsyncRead, AsyncWrite};
pub struct AsyncStdQueue(Async<Queue>);
impl AsyncStdQueue {
pub(crate) fn open(req: &IfReq) -> Result<Self> {
let queue = Queue::open(req)?;
let async_fd = Async::new(queue)?;
Ok(Self(async_fd))
}
#[inline]
pub fn close(&mut self) -> Result<()> {
self.0.get_mut().close()
}
#[inline]
pub async fn readable(&self) -> io::Result<()> {
self.0.readable().await
}
#[inline]
pub async fn writable(&self) -> io::Result<()> {
self.0.writable().await
}
#[inline]
pub fn get_ref(&self) -> &Queue {
self.0.get_ref()
}
#[inline]
pub async fn recv(&self, datagram: &mut [u8]) -> io::Result<usize> {
self.0.read_with(|queue| queue.recv(datagram)).await
}
#[inline]
pub async fn send(&self, datagram: &[u8]) -> io::Result<usize> {
self.0.write_with(|queue| queue.send(datagram)).await
}
}
impl AsyncWrite for AsyncStdQueue {
#[inline]
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
}
#[inline]
fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(Ok(()))
}
#[inline]
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Pin::new(&mut self.get_mut().0).poll_close(cx)
}
}
impl AsyncRead for AsyncStdQueue {
#[inline]
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> std::task::Poll<io::Result<usize>> {
Pin::new(&mut self.get_mut().0).poll_read(cx, buf)
}
}
impl Opener for AsyncStdQueue {
#[inline]
fn open(req: &IfReq) -> Result<Self> {
Self::open(req)
}
}