Skip to content

Commit 61a8adf

Browse files
committed
Implement I/O safety traits
1 parent bbed121 commit 61a8adf

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/file.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,22 @@ impl AsRawHandle for RandomAccessFile {
289289
}
290290
}
291291

292+
#[cfg(any(unix, target_os = "wasi"))]
293+
impl AsFd for RandomAccessFile {
294+
#[inline]
295+
fn as_fd(&self) -> BorrowedFd<'_> {
296+
self.0.as_fd()
297+
}
298+
}
299+
300+
#[cfg(target_os = "windows")]
301+
impl AsHandle for RandomAccessFile {
302+
#[inline]
303+
fn as_handle(&self) -> BorrowedHandle<'_> {
304+
self.0.as_handle()
305+
}
306+
}
307+
292308
#[cfg(any(unix, target_os = "wasi"))]
293309
impl FromRawFd for RandomAccessFile {
294310
#[inline]
@@ -305,6 +321,22 @@ impl FromRawHandle for RandomAccessFile {
305321
}
306322
}
307323

324+
#[cfg(any(unix, target_os = "wasi"))]
325+
impl From<OwnedFd> for RandomAccessFile {
326+
#[inline]
327+
fn from(fd: OwnedFd) -> Self {
328+
Self::from(File::from(fd))
329+
}
330+
}
331+
332+
#[cfg(target_os = "windows")]
333+
impl From<OwnedHandle> for RandomAccessFile {
334+
#[inline]
335+
fn from(handle: OwnedHandle) -> Self {
336+
Self::from(File::from(handle))
337+
}
338+
}
339+
308340
#[cfg(any(unix, target_os = "wasi"))]
309341
impl IntoRawFd for RandomAccessFile {
310342
#[inline]
@@ -321,6 +353,22 @@ impl IntoRawHandle for RandomAccessFile {
321353
}
322354
}
323355

356+
#[cfg(any(unix, target_os = "wasi"))]
357+
impl From<RandomAccessFile> for OwnedFd {
358+
#[inline]
359+
fn from(f: RandomAccessFile) -> Self {
360+
f.0.into()
361+
}
362+
}
363+
364+
#[cfg(target_os = "windows")]
365+
impl From<RandomAccessFile> for OwnedHandle {
366+
#[inline]
367+
fn from(f: RandomAccessFile) -> Self {
368+
f.0.into()
369+
}
370+
}
371+
324372
/// A file wrapper that is safe to use concurrently.
325373
///
326374
/// This wrapper exists because [`std::fs::File`] uses a single cursor, so
@@ -488,6 +536,22 @@ impl AsRawHandle for SyncFile {
488536
}
489537
}
490538

539+
#[cfg(any(unix, target_os = "wasi"))]
540+
impl AsFd for SyncFile {
541+
#[inline]
542+
fn as_fd(&self) -> BorrowedFd<'_> {
543+
self.0.get_ref().as_fd()
544+
}
545+
}
546+
547+
#[cfg(target_os = "windows")]
548+
impl AsHandle for SyncFile {
549+
#[inline]
550+
fn as_handle(&self) -> BorrowedHandle<'_> {
551+
self.0.get_ref().as_handle()
552+
}
553+
}
554+
491555
#[cfg(any(unix, target_os = "wasi"))]
492556
impl FromRawFd for SyncFile {
493557
#[inline]
@@ -504,6 +568,22 @@ impl FromRawHandle for SyncFile {
504568
}
505569
}
506570

571+
#[cfg(any(unix, target_os = "wasi"))]
572+
impl From<OwnedFd> for SyncFile {
573+
#[inline]
574+
fn from(fd: OwnedFd) -> Self {
575+
Self::from(File::from(fd))
576+
}
577+
}
578+
579+
#[cfg(target_os = "windows")]
580+
impl From<OwnedHandle> for SyncFile {
581+
#[inline]
582+
fn from(handle: OwnedHandle) -> Self {
583+
Self::from(File::from(handle))
584+
}
585+
}
586+
507587
impl fmt::Debug for SyncFile {
508588
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
509589
f.debug_struct("SyncFile")

0 commit comments

Comments
 (0)