use crate as orengine;
use crate::io::io_request_data::IoRequestData;
use crate::io::sys::{AsRawFd, RawFd};
use crate::io::worker::{local_worker, IoWorker};
use crate::io::{Buffer, FixedBufferMut};
use orengine_macros::poll_for_io_request;
use std::future::Future;
use std::io::Result;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct ReadBytes<'buf> {
fd: RawFd,
buf: &'buf mut [u8],
io_request_data: Option<IoRequestData>,
}
impl<'buf> ReadBytes<'buf> {
pub fn new(fd: RawFd, buf: &'buf mut [u8]) -> Self {
Self {
fd,
buf,
io_request_data: None,
}
}
}
impl Future for ReadBytes<'_> {
type Output = Result<usize>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never read more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let ret;
poll_for_io_request!((
local_worker().read(
this.fd,
this.buf.as_mut_ptr(),
this.buf.len() as u32,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() }
),
ret
));
}
}
unsafe impl Send for ReadBytes<'_> {}
pub struct ReadFixed<'buf> {
fd: RawFd,
ptr: *mut u8,
len: u32,
fixed_index: u16,
io_request_data: Option<IoRequestData>,
phantom_data: PhantomData<&'buf Buffer>,
}
impl ReadFixed<'_> {
pub fn new(fd: RawFd, ptr: *mut u8, len: u32, fixed_index: u16) -> Self {
Self {
fd,
ptr,
len,
fixed_index,
io_request_data: None,
phantom_data: PhantomData,
}
}
}
impl Future for ReadFixed<'_> {
type Output = Result<u32>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never read more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let ret;
poll_for_io_request!((
local_worker().read_fixed(this.fd, this.ptr, this.len, this.fixed_index, unsafe {
this.io_request_data.as_mut().unwrap_unchecked()
}),
ret as u32
));
}
}
unsafe impl Send for ReadFixed<'_> {}
pub struct PositionedReadBytes<'buf> {
fd: RawFd,
buf: &'buf mut [u8],
offset: usize,
io_request_data: Option<IoRequestData>,
}
impl<'buf> PositionedReadBytes<'buf> {
pub fn new(fd: RawFd, buf: &'buf mut [u8], offset: usize) -> Self {
Self {
fd,
buf,
offset,
io_request_data: None,
}
}
}
impl Future for PositionedReadBytes<'_> {
type Output = Result<usize>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never read more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let ret;
poll_for_io_request!((
local_worker().pread(
this.fd,
this.buf.as_mut_ptr(),
this.buf.len() as u32,
this.offset,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() }
),
ret
));
}
}
unsafe impl Send for PositionedReadBytes<'_> {}
pub struct PositionedReadFixed<'buf> {
fd: RawFd,
ptr: *mut u8,
len: u32,
fixed_index: u16,
offset: usize,
io_request_data: Option<IoRequestData>,
phantom_data: PhantomData<&'buf Buffer>,
}
impl PositionedReadFixed<'_> {
pub fn new(fd: RawFd, ptr: *mut u8, len: u32, fixed_index: u16, offset: usize) -> Self {
Self {
fd,
ptr,
len,
fixed_index,
offset,
io_request_data: None,
phantom_data: PhantomData,
}
}
}
impl Future for PositionedReadFixed<'_> {
type Output = Result<u32>;
#[allow(
clippy::cast_possible_truncation,
reason = "It never read more than u32::MAX bytes"
)]
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
let ret;
poll_for_io_request!((
local_worker().pread_fixed(
this.fd,
this.ptr,
this.len,
this.fixed_index,
this.offset,
unsafe { this.io_request_data.as_mut().unwrap_unchecked() }
),
ret as u32
));
}
}
unsafe impl Send for PositionedReadFixed<'_> {}
pub trait AsyncRead: AsRawFd {
#[inline(always)]
fn read_bytes(&mut self, buf: &mut [u8]) -> impl Future<Output = Result<usize>> {
ReadBytes::new(self.as_raw_fd(), buf)
}
#[inline(always)]
async fn read(&mut self, buf: &mut impl FixedBufferMut) -> Result<u32> {
if buf.is_fixed() {
ReadFixed::new(
self.as_raw_fd(),
buf.as_mut_ptr(),
buf.len_u32(),
buf.fixed_index(),
)
.await
} else {
#[allow(
clippy::cast_possible_truncation,
reason = "It never read more than u32::MAX bytes"
)]
ReadBytes::new(self.as_raw_fd(), buf.as_bytes_mut())
.await
.map(|r| r as u32)
}
}
#[inline(always)]
fn pread_bytes(
&mut self,
buf: &mut [u8],
offset: usize,
) -> impl Future<Output = Result<usize>> {
PositionedReadBytes::new(self.as_raw_fd(), buf, offset)
}
#[inline(always)]
async fn pread(&mut self, buf: &mut impl FixedBufferMut, offset: usize) -> Result<u32> {
if buf.is_fixed() {
PositionedReadFixed::new(
self.as_raw_fd(),
buf.as_mut_ptr(),
buf.len_u32(),
buf.fixed_index(),
offset,
)
.await
} else {
#[allow(
clippy::cast_possible_truncation,
reason = "It never read more than u32::MAX bytes"
)]
PositionedReadBytes::new(self.as_raw_fd(), buf.as_bytes_mut(), offset)
.await
.map(|ret| ret as u32)
}
}
#[inline(always)]
async fn read_bytes_exact(&mut self, buf: &mut [u8]) -> Result<()> {
let mut read = 0;
while read < buf.len() {
read += self.read_bytes(&mut buf[read..]).await?;
}
Ok(())
}
#[inline(always)]
async fn read_exact(&mut self, buf: &mut impl FixedBufferMut) -> Result<()> {
if buf.is_fixed() {
let mut read = 0;
#[allow(
clippy::cast_possible_wrap,
reason = "We believe it never read u32::MAX bytes"
)]
while read < buf.len_u32() {
read += ReadFixed::new(
self.as_raw_fd(),
unsafe { buf.as_mut_ptr().offset(read as isize) },
buf.len_u32() - read,
buf.fixed_index(),
)
.await?;
}
} else {
let mut read = 0;
let slice = buf.as_bytes_mut();
while read < slice.len() {
read += self.read_bytes(&mut slice[read..]).await?;
}
}
Ok(())
}
#[inline(always)]
async fn pread_bytes_exact(&mut self, buf: &mut [u8], offset: usize) -> Result<()> {
let mut read = 0;
while read < buf.len() {
read += self.pread_bytes(&mut buf[read..], offset + read).await?;
}
Ok(())
}
#[inline(always)]
async fn pread_exact(&mut self, buf: &mut impl FixedBufferMut, offset: usize) -> Result<()> {
if buf.is_fixed() {
let mut read = 0;
#[allow(
clippy::cast_possible_wrap,
reason = "We believe it never read u32::MAX bytes"
)]
while read < buf.len_u32() {
read += PositionedReadFixed::new(
self.as_raw_fd(),
unsafe { buf.as_mut_ptr().offset(read as isize) },
buf.len_u32() - read,
buf.fixed_index(),
offset + read as usize,
)
.await?;
}
} else {
let mut read = 0;
let slice = buf.as_bytes_mut();
while read < slice.len() {
read += self.pread_bytes(&mut slice[read..], offset + read).await?;
}
}
Ok(())
}
}