Skip to content

Commit

Permalink
Implement I/O safety traits
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Jan 8, 2024
1 parent bbed121 commit 61a8adf
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -305,6 +321,22 @@ impl FromRawHandle for RandomAccessFile {
}
}

#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for RandomAccessFile {
#[inline]
fn from(fd: OwnedFd) -> Self {
Self::from(File::from(fd))
}
}

#[cfg(target_os = "windows")]
impl From<OwnedHandle> for RandomAccessFile {
#[inline]
fn from(handle: OwnedHandle) -> Self {
Self::from(File::from(handle))
}
}

#[cfg(any(unix, target_os = "wasi"))]
impl IntoRawFd for RandomAccessFile {
#[inline]
Expand All @@ -321,6 +353,22 @@ impl IntoRawHandle for RandomAccessFile {
}
}

#[cfg(any(unix, target_os = "wasi"))]
impl From<RandomAccessFile> for OwnedFd {
#[inline]
fn from(f: RandomAccessFile) -> Self {
f.0.into()
}
}

#[cfg(target_os = "windows")]
impl From<RandomAccessFile> 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
Expand Down Expand Up @@ -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]
Expand All @@ -504,6 +568,22 @@ impl FromRawHandle for SyncFile {
}
}

#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for SyncFile {
#[inline]
fn from(fd: OwnedFd) -> Self {
Self::from(File::from(fd))
}
}

#[cfg(target_os = "windows")]
impl From<OwnedHandle> 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")
Expand Down

0 comments on commit 61a8adf

Please sign in to comment.