use crate::fs::{Metadata, Permissions};
#[cfg(unix)]
use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use async_std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
use async_std::{
fs, io,
task::{Context, Poll},
};
use std::{fmt, pin::Pin};
pub struct File {
cap_std: crate::fs::File,
}
impl File {
#[inline]
pub fn from_std(std: fs::File) -> Self {
Self::from_cap_std(crate::fs::File::from_std(std))
}
#[inline]
pub fn from_cap_std(cap_std: crate::fs::File) -> Self {
Self { cap_std }
}
#[inline]
pub async fn sync_all(&self) -> io::Result<()> {
self.cap_std.sync_all().await
}
#[inline]
pub async fn sync_data(&self) -> io::Result<()> {
self.cap_std.sync_data().await
}
#[inline]
pub async fn set_len(&self, size: u64) -> io::Result<()> {
self.cap_std.set_len(size).await
}
#[inline]
pub async fn metadata(&self) -> io::Result<Metadata> {
self.cap_std.metadata().await
}
#[inline]
pub async fn set_permissions(&self, perm: Permissions) -> io::Result<()> {
self.cap_std.set_permissions(perm).await
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromRawFd for File {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> Self {
Self::from_std(fs::File::from_raw_fd(fd))
}
}
#[cfg(windows)]
impl FromRawHandle for File {
#[inline]
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
Self::from_std(fs::File::from_raw_handle(handle))
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsRawFd for File {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.cap_std.as_raw_fd()
}
}
#[cfg(windows)]
impl AsRawHandle for File {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.cap_std.as_raw_handle()
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoRawFd for File {
#[inline]
fn into_raw_fd(self) -> RawFd {
self.cap_std.into_raw_fd()
}
}
#[cfg(windows)]
impl IntoRawHandle for File {
#[inline]
fn into_raw_handle(self) -> RawHandle {
self.cap_std.into_raw_handle()
}
}
impl io::Read for File {
#[inline]
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
io::Read::poll_read(Pin::new(&mut self.cap_std), cx, buf)
}
#[inline]
fn poll_read_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context,
bufs: &mut [io::IoSliceMut],
) -> Poll<io::Result<usize>> {
io::Read::poll_read_vectored(Pin::new(&mut self.cap_std), cx, bufs)
}
}
impl io::Write for File {
#[inline]
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut Context,
buf: &[u8],
) -> Poll<io::Result<usize>> {
io::Write::poll_write(Pin::new(&mut self.cap_std), cx, buf)
}
#[inline]
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
io::Write::poll_flush(Pin::new(&mut self.cap_std), cx)
}
#[inline]
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
io::Write::poll_close(Pin::new(&mut self.cap_std), cx)
}
#[inline]
fn poll_write_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context,
bufs: &[io::IoSlice],
) -> Poll<io::Result<usize>> {
io::Write::poll_write_vectored(Pin::new(&mut self.cap_std), cx, bufs)
}
}
impl io::Seek for File {
#[inline]
fn poll_seek(
mut self: Pin<&mut Self>,
cx: &mut Context,
pos: io::SeekFrom,
) -> Poll<io::Result<u64>> {
io::Seek::poll_seek(Pin::new(&mut self.cap_std), cx, pos)
}
}
impl fmt::Debug for File {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.cap_std.fmt(f)
}
}