diff --git a/src/file.rs b/src/file.rs index 521ac25..874f81b 100644 --- a/src/file.rs +++ b/src/file.rs @@ -289,6 +289,22 @@ impl AsRawHandle for RandomAccessFile { } } +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for RandomAccessFile { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + self.0.as_fd() + } +} + +#[cfg(target_os = "windows")] +impl AsHandle for RandomAccessFile { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + self.0.as_handle() + } +} + #[cfg(any(unix, target_os = "wasi"))] impl FromRawFd for RandomAccessFile { #[inline] @@ -305,6 +321,22 @@ impl FromRawHandle for RandomAccessFile { } } +#[cfg(any(unix, target_os = "wasi"))] +impl From for RandomAccessFile { + #[inline] + fn from(fd: OwnedFd) -> Self { + Self::from(File::from(fd)) + } +} + +#[cfg(target_os = "windows")] +impl From for RandomAccessFile { + #[inline] + fn from(handle: OwnedHandle) -> Self { + Self::from(File::from(handle)) + } +} + #[cfg(any(unix, target_os = "wasi"))] impl IntoRawFd for RandomAccessFile { #[inline] @@ -321,6 +353,22 @@ impl IntoRawHandle for RandomAccessFile { } } +#[cfg(any(unix, target_os = "wasi"))] +impl From for OwnedFd { + #[inline] + fn from(f: RandomAccessFile) -> Self { + f.0.into() + } +} + +#[cfg(target_os = "windows")] +impl From for OwnedHandle { + #[inline] + fn from(f: RandomAccessFile) -> Self { + f.0.into() + } +} + /// A file wrapper that is safe to use concurrently. /// /// This wrapper exists because [`std::fs::File`] uses a single cursor, so @@ -488,6 +536,22 @@ impl AsRawHandle for SyncFile { } } +#[cfg(any(unix, target_os = "wasi"))] +impl AsFd for SyncFile { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + self.0.get_ref().as_fd() + } +} + +#[cfg(target_os = "windows")] +impl AsHandle for SyncFile { + #[inline] + fn as_handle(&self) -> BorrowedHandle<'_> { + self.0.get_ref().as_handle() + } +} + #[cfg(any(unix, target_os = "wasi"))] impl FromRawFd for SyncFile { #[inline] @@ -504,6 +568,22 @@ impl FromRawHandle for SyncFile { } } +#[cfg(any(unix, target_os = "wasi"))] +impl From for SyncFile { + #[inline] + fn from(fd: OwnedFd) -> Self { + Self::from(File::from(fd)) + } +} + +#[cfg(target_os = "windows")] +impl From for SyncFile { + #[inline] + fn from(handle: OwnedHandle) -> Self { + Self::from(File::from(handle)) + } +} + impl fmt::Debug for SyncFile { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SyncFile")