From 6d7211738d74436a5d36ca68948d86f4809b9001 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 27 Jul 2021 17:15:40 -0700 Subject: [PATCH] Add Safety comments to the `As*` for `Owned*` implementations. --- library/std/src/os/unix/io/fd.rs | 3 +++ library/std/src/os/wasi/io/fd.rs | 3 +++ library/std/src/os/windows/io/handle.rs | 3 +++ library/std/src/os/windows/io/socket.rs | 3 +++ 4 files changed, 12 insertions(+) diff --git a/library/std/src/os/unix/io/fd.rs b/library/std/src/os/unix/io/fd.rs index 2be6198092f..22b1151b92a 100644 --- a/library/std/src/os/unix/io/fd.rs +++ b/library/std/src/os/unix/io/fd.rs @@ -178,6 +178,9 @@ fn as_fd(&self) -> BorrowedFd<'_> { impl AsFd for OwnedFd { #[inline] fn as_fd(&self) -> BorrowedFd<'_> { + // Safety: `OwnedFd` and `BorrowedFd` have the same validity + // invariants, and the `BorrowdFd` is bounded by the lifetime + // of `&self`. unsafe { BorrowedFd::borrow_raw_fd(self.as_raw_fd()) } } } diff --git a/library/std/src/os/wasi/io/fd.rs b/library/std/src/os/wasi/io/fd.rs index f77a73abb90..e07c2e12b7d 100644 --- a/library/std/src/os/wasi/io/fd.rs +++ b/library/std/src/os/wasi/io/fd.rs @@ -177,6 +177,9 @@ fn as_fd(&self) -> BorrowedFd<'_> { impl AsFd for OwnedFd { #[inline] fn as_fd(&self) -> BorrowedFd<'_> { + // Safety: `OwnedFd` and `BorrowedFd` have the same validity + // invariants, and the `BorrowdFd` is bounded by the lifetime + // of `&self`. unsafe { BorrowedFd::borrow_raw_fd(self.as_raw_fd()) } } } diff --git a/library/std/src/os/windows/io/handle.rs b/library/std/src/os/windows/io/handle.rs index 5d250520685..87fbd3e0460 100644 --- a/library/std/src/os/windows/io/handle.rs +++ b/library/std/src/os/windows/io/handle.rs @@ -282,6 +282,9 @@ fn as_handle(&self) -> BorrowedHandle<'_> { impl AsHandle for OwnedHandle { #[inline] fn as_handle(&self) -> BorrowedHandle<'_> { + // Safety: `OwnedHandle` and `BorrowedHandle` have the same validity + // invariants, and the `BorrowdHandle` is bounded by the lifetime + // of `&self`. unsafe { BorrowedHandle::borrow_raw_handle(self.as_raw_handle()) } } } diff --git a/library/std/src/os/windows/io/socket.rs b/library/std/src/os/windows/io/socket.rs index fd89f4cc60c..23db66df09f 100644 --- a/library/std/src/os/windows/io/socket.rs +++ b/library/std/src/os/windows/io/socket.rs @@ -145,6 +145,9 @@ fn as_socket(&self) -> BorrowedSocket<'_> { impl AsSocket for OwnedSocket { #[inline] fn as_socket(&self) -> BorrowedSocket<'_> { + // Safety: `OwnedSocket` and `BorrowedSocket` have the same validity + // invariants, and the `BorrowdSocket` is bounded by the lifetime + // of `&self`. unsafe { BorrowedSocket::borrow_raw_socket(self.as_raw_socket()) } } }