From 7dd2fed7797ce90264b6fc2a8940b0d97e01b3a5 Mon Sep 17 00:00:00 2001 From: Bruce Leidl Date: Wed, 11 Sep 2019 11:30:44 -0400 Subject: [PATCH] implement AsRawFd for EventFd and IoEventFd --- rust/src/virtio/eventfd.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/rust/src/virtio/eventfd.rs b/rust/src/virtio/eventfd.rs index ff32f60..7be3e6c 100644 --- a/rust/src/virtio/eventfd.rs +++ b/rust/src/virtio/eventfd.rs @@ -1,5 +1,5 @@ use std::sync::Arc; -use std::os::unix::io::RawFd; +use std::os::unix::io::{RawFd,AsRawFd}; use libc; @@ -19,10 +19,6 @@ impl EventFd { Ok(EventFd(fd)) } - pub fn raw_fd(&self) -> RawFd { - self.0 - } - pub fn write(&self, v: u64) -> Result<()> { let ret = unsafe { libc::write(self.0, &v as *const _ as *const libc::c_void, U64_SZ) }; if ret as usize != U64_SZ { @@ -53,6 +49,12 @@ impl Drop for EventFd { } } +impl AsRawFd for EventFd { + fn as_raw_fd(&self) -> RawFd { + self.0 + } +} + pub struct IoEventFd { kvm: Kvm, addr: u64, @@ -62,7 +64,7 @@ pub struct IoEventFd { impl IoEventFd { pub fn new(kvm: &Kvm, address: u64) -> Result { let evt = Arc::new(EventFd::new()?); - kvm.ioeventfd_add(address, evt.raw_fd())?; + kvm.ioeventfd_add(address, evt.as_raw_fd())?; Ok(IoEventFd { kvm: kvm.clone(), addr: address, @@ -76,12 +78,16 @@ impl IoEventFd { pub fn write(&self, v: u64) -> Result<()> { self.evt.write(v) } - } impl Drop for IoEventFd { fn drop(&mut self) { - let _ = self.kvm.ioeventfd_del(self.addr, self.evt.raw_fd()); + let _ = self.kvm.ioeventfd_del(self.addr, self.evt.as_raw_fd()); } } +impl AsRawFd for IoEventFd { + fn as_raw_fd(&self) -> RawFd { + self.evt.as_raw_fd() + } +}