From 6cb5afe83f6b44b02b06ca555ebee18fc5a1b9df Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Wed, 30 Jul 2025 10:35:53 +0200 Subject: [PATCH] vhost: impl From for Listener It was already possible to do this as follows: unsafe { Listener::from_raw_fd(listener.into_raw_fd()) } This just factors the From implementation out of the FromRawFd implementation so it's possible to do the conversion without an unnecessary unsafe in application code. Signed-off-by: Alyssa Ross --- vhost/CHANGELOG.md | 3 +++ vhost/src/vhost_user/connection.rs | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/vhost/CHANGELOG.md b/vhost/CHANGELOG.md index aa13a7be..8bc85b59 100644 --- a/vhost/CHANGELOG.md +++ b/vhost/CHANGELOG.md @@ -3,6 +3,9 @@ ## [Unreleased] ### Added +- [[#311]](https://github.com/rust-vmm/vhost/pull/311) Implement + `From` for `vhost_user::Listener`. + ### Changed ### Deprecated ### Fixed diff --git a/vhost/src/vhost_user/connection.rs b/vhost/src/vhost_user/connection.rs index 4fca9c5b..3485e78a 100644 --- a/vhost/src/vhost_user/connection.rs +++ b/vhost/src/vhost_user/connection.rs @@ -86,10 +86,13 @@ impl AsRawFd for Listener { impl FromRawFd for Listener { unsafe fn from_raw_fd(fd: RawFd) -> Self { - Listener { - fd: UnixListener::from_raw_fd(fd), - path: None, - } + Self::from(UnixListener::from_raw_fd(fd)) + } +} + +impl From for Listener { + fn from(fd: UnixListener) -> Self { + Self { fd, path: None } } }