|
|
|
@ -15,6 +15,8 @@ use crate::prelude::*;
|
|
|
|
|
use crate::task::{spawn_blocking, Context, Poll, Waker};
|
|
|
|
|
use crate::utils::Context as _;
|
|
|
|
|
|
|
|
|
|
const ARC_TRY_UNWRAP_EXPECT: &str = "cannot acquire ownership of the file handle after drop";
|
|
|
|
|
|
|
|
|
|
/// An open file on the filesystem.
|
|
|
|
|
///
|
|
|
|
|
/// Depending on what options the file was opened with, this type can be used for reading and/or
|
|
|
|
@ -415,6 +417,15 @@ impl From<std::fs::File> for File {
|
|
|
|
|
cfg_unix! {
|
|
|
|
|
use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
|
|
|
|
|
|
|
|
|
|
impl File {
|
|
|
|
|
fn into_std_file(self) -> std::fs::File {
|
|
|
|
|
let file = self.file.clone();
|
|
|
|
|
drop(self);
|
|
|
|
|
Arc::try_unwrap(file)
|
|
|
|
|
.expect(ARC_TRY_UNWRAP_EXPECT)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl AsRawFd for File {
|
|
|
|
|
fn as_raw_fd(&self) -> RawFd {
|
|
|
|
|
self.file.as_raw_fd()
|
|
|
|
@ -429,11 +440,29 @@ cfg_unix! {
|
|
|
|
|
|
|
|
|
|
impl IntoRawFd for File {
|
|
|
|
|
fn into_raw_fd(self) -> RawFd {
|
|
|
|
|
let file = self.file.clone();
|
|
|
|
|
drop(self);
|
|
|
|
|
Arc::try_unwrap(file)
|
|
|
|
|
.expect("cannot acquire ownership of the file handle after drop")
|
|
|
|
|
.into_raw_fd()
|
|
|
|
|
self.into_std_file().into_raw_fd()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg_io_safety! {
|
|
|
|
|
use crate::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
|
|
|
|
|
|
|
|
|
|
impl AsFd for File {
|
|
|
|
|
fn as_fd(&self) -> BorrowedFd<'_> {
|
|
|
|
|
self.file.as_fd()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<OwnedFd> for File {
|
|
|
|
|
fn from(fd: OwnedFd) -> Self {
|
|
|
|
|
std::fs::File::from(fd).into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<File> for OwnedFd {
|
|
|
|
|
fn from(val: File) -> OwnedFd {
|
|
|
|
|
self.into_std_file().into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -458,10 +487,36 @@ cfg_windows! {
|
|
|
|
|
let file = self.file.clone();
|
|
|
|
|
drop(self);
|
|
|
|
|
Arc::try_unwrap(file)
|
|
|
|
|
.expect("cannot acquire ownership of the file handle after drop")
|
|
|
|
|
.expect(ARC_TRY_UNWRAP_EXPECT)
|
|
|
|
|
.into_raw_handle()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cfg_io_safety! {
|
|
|
|
|
use crate::os::windows::io::{AsHandle, BorrowedHandle, OwnedHandle};
|
|
|
|
|
|
|
|
|
|
impl AsHandle for File {
|
|
|
|
|
fn as_handle(&self) -> BorrowedHandle<'_> {
|
|
|
|
|
self.file.as_handle()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<OwnedHandle> for File {
|
|
|
|
|
fn from(handle: OwnedHandle) -> Self {
|
|
|
|
|
std::fs::File::from(handle).into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<File> for OwnedHandle {
|
|
|
|
|
fn from(val: File) -> OwnedHandle {
|
|
|
|
|
let file = val.file.clone();
|
|
|
|
|
drop(val);
|
|
|
|
|
Arc::try_unwrap(file)
|
|
|
|
|
.expect(ARC_TRY_UNWRAP_EXPECT)
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// An async mutex with non-borrowing lock guards.
|
|
|
|
|