diff --git a/common/sys_util/src/descriptor.rs b/common/sys_util/src/descriptor.rs index 45ae618e64..b83bfd47da 100644 --- a/common/sys_util/src/descriptor.rs +++ b/common/sys_util/src/descriptor.rs @@ -13,7 +13,7 @@ use std::os::unix::net::{UnixDatagram, UnixStream}; use serde::{Deserialize, Serialize}; -use crate::net::UnlinkUnixSeqpacketListener; +use crate::net::{UnixSeqpacket, UnlinkUnixSeqpacketListener}; use crate::{errno_result, PollToken, Result}; pub type RawDescriptor = RawFd; @@ -168,6 +168,13 @@ impl From for UnixStream { } } +impl From for SafeDescriptor { + fn from(s: UnixSeqpacket) -> Self { + // Safe because we own the UnixSeqpacket at this point. + unsafe { SafeDescriptor::from_raw_descriptor(s.into_raw_descriptor()) } + } +} + /// For use cases where a simple wrapper around a RawDescriptor is needed. /// This is a simply a wrapper and does not manage the lifetime of the descriptor. /// Most usages should prefer SafeDescriptor or using a RawDescriptor directly diff --git a/common/sys_util/src/net.rs b/common/sys_util/src/net.rs index 294f1155e3..ef703f42a9 100644 --- a/common/sys_util/src/net.rs +++ b/common/sys_util/src/net.rs @@ -27,7 +27,7 @@ use crate::{ sock_ctrl_msg::{ScmSocket, SCM_SOCKET_MAX_FD_COUNT}, FromRawDescriptor, }; -use crate::{AsRawDescriptor, RawDescriptor}; +use crate::{AsRawDescriptor, IntoRawDescriptor, RawDescriptor}; /// Assist in handling both IP version 4 and IP version 6. #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -614,6 +614,14 @@ impl FromRawDescriptor for UnixSeqpacket { } } +impl IntoRawDescriptor for UnixSeqpacket { + fn into_raw_descriptor(self) -> RawDescriptor { + let fd = self.fd; + mem::forget(self); + fd + } +} + impl AsRawFd for UnixSeqpacket { fn as_raw_fd(&self) -> RawFd { self.fd