From a51e795f85932e1dc324dbab7d7ee312e5d65c9f Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Mon, 30 Mar 2026 12:17:54 +0100 Subject: [PATCH] Implement conversions between OwnedFd and vsock types. This allows a VsockListener, VsockSocket or VsockStream to be converted to an OwnedFd and vice-versa. Signed-off-by: Andrew Walbran --- src/lib.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1e1709f..623f503 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -237,6 +237,18 @@ impl IntoRawFd for VsockListener { } } +impl From for OwnedFd { + fn from(value: VsockListener) -> Self { + value.socket + } +} + +impl From for VsockListener { + fn from(socket: OwnedFd) -> Self { + Self { socket } + } +} + /// A virtio sequential packet socket between a local and a remote host. /// /// This is the vsock equivalent of [`std::net::UdpSocket`]. @@ -407,6 +419,18 @@ impl IntoRawFd for VsockSocket { } } +impl From for OwnedFd { + fn from(value: VsockSocket) -> Self { + value.socket + } +} + +impl From for VsockSocket { + fn from(socket: OwnedFd) -> Self { + Self { socket } + } +} + /// A virtio stream between a local and a remote socket. /// /// This is the vsock equivalent of [`std::net::TcpStream`]. @@ -554,6 +578,18 @@ impl IntoRawFd for VsockStream { } } +impl From for OwnedFd { + fn from(value: VsockStream) -> Self { + value.socket + } +} + +impl From for VsockStream { + fn from(socket: OwnedFd) -> Self { + Self { socket } + } +} + const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9; ioctl_read_bad!( vm_sockets_get_local_cid,