From 14f42a9aa27fff2ca8579153ed79b845627835df Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 9 Mar 2026 17:42:03 +0000 Subject: [PATCH] activation: Implement From for OwnedFd I think this crate predates I/O safety in std, but now that `OwnedFd` is part of std we can use it. I want to have most of my crates use `#[forbid(unsafe_code)]` and this helps. Assisted-by: OpenCode (claude-opus-4-6) Signed-off-by: Colin Walters --- src/activation.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/activation.rs b/src/activation.rs index adbd478..fae8bfb 100644 --- a/src/activation.rs +++ b/src/activation.rs @@ -5,7 +5,7 @@ use nix::sys::socket::{AddressFamily, SockaddrLike, SockaddrStorage}; use nix::sys::stat::fstat; use std::convert::TryFrom; use std::env; -use std::os::unix::io::{IntoRawFd, RawFd}; +use std::os::unix::io::{FromRawFd, IntoRawFd, OwnedFd, RawFd}; use std::process; /// Minimum FD number used by systemd for passing sockets. @@ -271,6 +271,14 @@ impl IntoRawFd for FileDescriptor { } } +impl From for OwnedFd { + fn from(fd: FileDescriptor) -> Self { + // SAFETY: FileDescriptor owns its fd, and IntoRawFd consumes it, + // so we have exclusive ownership of the raw fd at this point. + unsafe { OwnedFd::from_raw_fd(fd.into_raw_fd()) } + } +} + #[cfg(test)] mod tests { use super::*;