Skip to content

Commit

Permalink
Added safety comments above unsafe blocks within safe functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO authored and haraldh committed Dec 2, 2023
1 parent 14f3cbe commit df87259
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/stdnet/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,21 @@ impl IntoRawSocket for Socket {

impl AsSocket for Socket {
fn as_socket(&self) -> BorrowedSocket<'_> {
// SAFETY: Although the lifetime is elided, it is indeed a borrow from self, and the returned value can not outlive the lifetime of the Socket.
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}

impl From<Socket> for OwnedSocket {
fn from(sock: Socket) -> OwnedSocket {
// SAFETY: This is safe because it consumes the socket using the `OwnedSocket::from(Socket)`, or by using `Socket.into::<OwnedSocket>()`
unsafe { OwnedSocket::from_raw_socket(sock.into_raw_socket()) }
}
}

impl From<OwnedSocket> for Socket {
fn from(owned: OwnedSocket) -> Socket {
// SAFETY: This is safe because it consumes the socket using the `Socket::from(OwnedSocket)`, or by using `OwnedSocket.into::<Socket>()`
unsafe { Socket::from_raw_socket(owned.into_raw_socket()) }
}
}

0 comments on commit df87259

Please sign in to comment.