Use std::io::Result to reduce duplicated code.

Use std::io::Result to reduce duplicated code.

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
Liu Jiang 2021-12-18 19:56:59 +08:00 committed by Jiang Liu
parent e5a5f1fe34
commit f805c3e1ff
3 changed files with 26 additions and 54 deletions

View file

@ -18,9 +18,8 @@
//! [VhostUserBackend]: trait.VhostUserBackend.html
//! [VhostUserBackendMut]: trait.VhostUserBackendMut.html
use std::io;
use std::io::Result;
use std::ops::Deref;
use std::result;
use std::sync::{Arc, Mutex, RwLock};
use vhost::vhost_user::message::VhostUserProtocolFeatures;
@ -71,12 +70,12 @@ where
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn set_config(&self, _offset: u32, _buf: &[u8]) -> result::Result<(), io::Error> {
fn set_config(&self, _offset: u32, _buf: &[u8]) -> Result<()> {
Ok(())
}
/// Update guest memory regions.
fn update_memory(&self, mem: GM<B>) -> result::Result<(), io::Error>;
fn update_memory(&self, mem: GM<B>) -> Result<()>;
/// Set handler for communicating with the master by the slave communication channel.
///
@ -115,7 +114,7 @@ where
evset: EventSet,
vrings: &[V],
thread_id: usize,
) -> result::Result<bool, io::Error>;
) -> Result<bool>;
}
/// Trait without interior mutability for vhost user backend servers to implement concrete services.
@ -154,12 +153,12 @@ where
///
/// A default implementation is provided as we cannot expect all backends to implement this
/// function.
fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> result::Result<(), io::Error> {
fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> Result<()> {
Ok(())
}
/// Update guest memory regions.
fn update_memory(&mut self, mem: GM<B>) -> result::Result<(), io::Error>;
fn update_memory(&mut self, mem: GM<B>) -> Result<()>;
/// Set handler for communicating with the master by the slave communication channel.
///
@ -198,7 +197,7 @@ where
evset: EventSet,
vrings: &[V],
thread_id: usize,
) -> result::Result<bool, io::Error>;
) -> Result<bool>;
}
impl<T: VhostUserBackend<V, B>, V, B> VhostUserBackend<V, B> for Arc<T>
@ -234,11 +233,11 @@ where
self.deref().get_config(offset, size)
}
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<(), io::Error> {
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
self.deref().set_config(offset, buf)
}
fn update_memory(&self, mem: GM<B>) -> Result<(), io::Error> {
fn update_memory(&self, mem: GM<B>) -> Result<()> {
self.deref().update_memory(mem)
}
@ -260,7 +259,7 @@ where
evset: EventSet,
vrings: &[V],
thread_id: usize,
) -> Result<bool, io::Error> {
) -> Result<bool> {
self.deref()
.handle_event(device_event, evset, vrings, thread_id)
}
@ -299,11 +298,11 @@ where
self.lock().unwrap().get_config(offset, size)
}
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<(), io::Error> {
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
self.lock().unwrap().set_config(offset, buf)
}
fn update_memory(&self, mem: GM<B>) -> Result<(), io::Error> {
fn update_memory(&self, mem: GM<B>) -> Result<()> {
self.lock().unwrap().update_memory(mem)
}
@ -325,7 +324,7 @@ where
evset: EventSet,
vrings: &[V],
thread_id: usize,
) -> Result<bool, io::Error> {
) -> Result<bool> {
self.lock()
.unwrap()
.handle_event(device_event, evset, vrings, thread_id)
@ -365,11 +364,11 @@ where
self.read().unwrap().get_config(offset, size)
}
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<(), io::Error> {
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
self.write().unwrap().set_config(offset, buf)
}
fn update_memory(&self, mem: GM<B>) -> Result<(), io::Error> {
fn update_memory(&self, mem: GM<B>) -> Result<()> {
self.write().unwrap().update_memory(mem)
}
@ -391,7 +390,7 @@ where
evset: EventSet,
vrings: &[V],
thread_id: usize,
) -> Result<bool, io::Error> {
) -> Result<bool> {
self.write()
.unwrap()
.handle_event(device_event, evset, vrings, thread_id)
@ -402,7 +401,6 @@ where
pub mod tests {
use super::*;
use crate::VringRwLock;
use std::io::Error;
use std::sync::Mutex;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
@ -454,7 +452,7 @@ pub mod tests {
vec![0xa5u8; 8]
}
fn set_config(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
fn set_config(&mut self, offset: u32, buf: &[u8]) -> Result<()> {
assert_eq!(offset, 0x200);
assert_eq!(buf.len(), 8);
assert_eq!(buf, &[0xa5u8; 8]);
@ -462,10 +460,7 @@ pub mod tests {
Ok(())
}
fn update_memory(
&mut self,
_atomic_mem: GuestMemoryAtomic<GuestMemoryMmap>,
) -> Result<(), Error> {
fn update_memory(&mut self, _atomic_mem: GuestMemoryAtomic<GuestMemoryMmap>) -> Result<()> {
Ok(())
}
@ -487,7 +482,7 @@ pub mod tests {
_evset: EventSet,
_vrings: &[VringRwLock],
_thread_id: usize,
) -> Result<bool, Error> {
) -> Result<bool> {
self.events += 1;
Ok(false)

View file

@ -4,10 +4,9 @@
// SPDX-License-Identifier: Apache-2.0
use std::fmt::{Display, Formatter};
use std::io;
use std::io::{self, Result};
use std::marker::PhantomData;
use std::os::unix::io::{AsRawFd, RawFd};
use std::result;
use vm_memory::bitmap::Bitmap;
use vmm_sys_util::epoll::{ControlOperation, Epoll, EpollEvent, EventSet};
@ -127,12 +126,7 @@ where
///
/// When this event is later triggered, the backend implementation of `handle_event` will be
/// called.
pub fn register_listener(
&self,
fd: RawFd,
ev_type: EventSet,
data: u64,
) -> result::Result<(), io::Error> {
pub fn register_listener(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
// `data` range [0...num_queues] is reserved for queues and exit event.
if data <= self.backend.num_queues() as u64 {
Err(io::Error::from_raw_os_error(libc::EINVAL))
@ -145,12 +139,7 @@ where
///
/// If the event is triggered after this function has been called, the event will be silently
/// dropped.
pub fn unregister_listener(
&self,
fd: RawFd,
ev_type: EventSet,
data: u64,
) -> result::Result<(), io::Error> {
pub fn unregister_listener(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
// `data` range [0...num_queues] is reserved for queues and exit event.
if data <= self.backend.num_queues() as u64 {
Err(io::Error::from_raw_os_error(libc::EINVAL))
@ -159,22 +148,12 @@ where
}
}
pub(crate) fn register_event(
&self,
fd: RawFd,
ev_type: EventSet,
data: u64,
) -> result::Result<(), io::Error> {
pub(crate) fn register_event(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
self.epoll
.ctl(ControlOperation::Add, fd, EpollEvent::new(ev_type, data))
}
pub(crate) fn unregister_event(
&self,
fd: RawFd,
ev_type: EventSet,
data: u64,
) -> result::Result<(), io::Error> {
pub(crate) fn unregister_event(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
self.epoll
.ctl(ControlOperation::Delete, fd, EpollEvent::new(ev_type, data))
}

View file

@ -9,8 +9,6 @@
extern crate log;
use std::fmt::{Display, Formatter};
use std::io;
use std::result;
use std::sync::{Arc, Mutex};
use std::thread;
@ -48,7 +46,7 @@ pub enum Error {
/// Failed creating vhost-user slave handler.
CreateSlaveReqHandler(VhostUserError),
/// Failed starting daemon thread.
StartDaemon(io::Error),
StartDaemon(std::io::Error),
/// Failed waiting for daemon thread.
WaitDaemon(std::boxed::Box<dyn std::any::Any + std::marker::Send>),
/// Failed handling a vhost-user request.
@ -69,7 +67,7 @@ impl Display for Error {
}
/// Result of vhost-user daemon operations.
pub type Result<T> = result::Result<T, Error>;
pub type Result<T> = std::result::Result<T, Error>;
/// Implement a simple framework to run a vhost-user service daemon.
///