implement AsRawFd for EventFd and IoEventFd

This commit is contained in:
Bruce Leidl 2019-09-11 11:30:44 -04:00
parent 1d7857199d
commit 7dd2fed779

View File

@ -1,5 +1,5 @@
use std::sync::Arc; use std::sync::Arc;
use std::os::unix::io::RawFd; use std::os::unix::io::{RawFd,AsRawFd};
use libc; use libc;
@ -19,10 +19,6 @@ impl EventFd {
Ok(EventFd(fd)) Ok(EventFd(fd))
} }
pub fn raw_fd(&self) -> RawFd {
self.0
}
pub fn write(&self, v: u64) -> Result<()> { pub fn write(&self, v: u64) -> Result<()> {
let ret = unsafe { libc::write(self.0, &v as *const _ as *const libc::c_void, U64_SZ) }; let ret = unsafe { libc::write(self.0, &v as *const _ as *const libc::c_void, U64_SZ) };
if ret as usize != 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 { pub struct IoEventFd {
kvm: Kvm, kvm: Kvm,
addr: u64, addr: u64,
@ -62,7 +64,7 @@ pub struct IoEventFd {
impl IoEventFd { impl IoEventFd {
pub fn new(kvm: &Kvm, address: u64) -> Result<IoEventFd> { pub fn new(kvm: &Kvm, address: u64) -> Result<IoEventFd> {
let evt = Arc::new(EventFd::new()?); let evt = Arc::new(EventFd::new()?);
kvm.ioeventfd_add(address, evt.raw_fd())?; kvm.ioeventfd_add(address, evt.as_raw_fd())?;
Ok(IoEventFd { Ok(IoEventFd {
kvm: kvm.clone(), kvm: kvm.clone(),
addr: address, addr: address,
@ -76,12 +78,16 @@ impl IoEventFd {
pub fn write(&self, v: u64) -> Result<()> { pub fn write(&self, v: u64) -> Result<()> {
self.evt.write(v) self.evt.write(v)
} }
} }
impl Drop for IoEventFd { impl Drop for IoEventFd {
fn drop(&mut self) { 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()
}
}