Skip to content

Commit 7149769

Browse files
committed
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 <qwandor@google.com>
1 parent 8e84f31 commit 7149769

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

src/lib.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ impl IntoRawFd for VsockListener {
237237
}
238238
}
239239

240+
impl From<VsockListener> for OwnedFd {
241+
fn from(value: VsockListener) -> Self {
242+
value.socket
243+
}
244+
}
245+
246+
impl From<OwnedFd> for VsockListener {
247+
fn from(socket: OwnedFd) -> Self {
248+
Self { socket }
249+
}
250+
}
251+
240252
/// A virtio sequential packet socket between a local and a remote host.
241253
///
242254
/// This is the vsock equivalent of [`std::net::UdpSocket`].
@@ -407,6 +419,18 @@ impl IntoRawFd for VsockSocket {
407419
}
408420
}
409421

422+
impl From<VsockSocket> for OwnedFd {
423+
fn from(value: VsockSocket) -> Self {
424+
value.socket
425+
}
426+
}
427+
428+
impl From<OwnedFd> for VsockSocket {
429+
fn from(socket: OwnedFd) -> Self {
430+
Self { socket }
431+
}
432+
}
433+
410434
/// A virtio stream between a local and a remote socket.
411435
///
412436
/// This is the vsock equivalent of [`std::net::TcpStream`].
@@ -554,6 +578,18 @@ impl IntoRawFd for VsockStream {
554578
}
555579
}
556580

581+
impl From<VsockStream> for OwnedFd {
582+
fn from(value: VsockStream) -> Self {
583+
value.socket
584+
}
585+
}
586+
587+
impl From<OwnedFd> for VsockStream {
588+
fn from(socket: OwnedFd) -> Self {
589+
Self { socket }
590+
}
591+
}
592+
557593
const IOCTL_VM_SOCKETS_GET_LOCAL_CID: usize = 0x7b9;
558594
ioctl_read_bad!(
559595
vm_sockets_get_local_cid,

0 commit comments

Comments
 (0)