Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.

Commit f805c3e

Browse files
committed
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>
1 parent e5a5f1f commit f805c3e

File tree

3 files changed

+26
-54
lines changed

3 files changed

+26
-54
lines changed

src/backend.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
//! [VhostUserBackend]: trait.VhostUserBackend.html
1919
//! [VhostUserBackendMut]: trait.VhostUserBackendMut.html
2020
21-
use std::io;
21+
use std::io::Result;
2222
use std::ops::Deref;
23-
use std::result;
2423
use std::sync::{Arc, Mutex, RwLock};
2524

2625
use vhost::vhost_user::message::VhostUserProtocolFeatures;
@@ -71,12 +70,12 @@ where
7170
///
7271
/// A default implementation is provided as we cannot expect all backends to implement this
7372
/// function.
74-
fn set_config(&self, _offset: u32, _buf: &[u8]) -> result::Result<(), io::Error> {
73+
fn set_config(&self, _offset: u32, _buf: &[u8]) -> Result<()> {
7574
Ok(())
7675
}
7776

7877
/// Update guest memory regions.
79-
fn update_memory(&self, mem: GM<B>) -> result::Result<(), io::Error>;
78+
fn update_memory(&self, mem: GM<B>) -> Result<()>;
8079

8180
/// Set handler for communicating with the master by the slave communication channel.
8281
///
@@ -115,7 +114,7 @@ where
115114
evset: EventSet,
116115
vrings: &[V],
117116
thread_id: usize,
118-
) -> result::Result<bool, io::Error>;
117+
) -> Result<bool>;
119118
}
120119

121120
/// Trait without interior mutability for vhost user backend servers to implement concrete services.
@@ -154,12 +153,12 @@ where
154153
///
155154
/// A default implementation is provided as we cannot expect all backends to implement this
156155
/// function.
157-
fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> result::Result<(), io::Error> {
156+
fn set_config(&mut self, _offset: u32, _buf: &[u8]) -> Result<()> {
158157
Ok(())
159158
}
160159

161160
/// Update guest memory regions.
162-
fn update_memory(&mut self, mem: GM<B>) -> result::Result<(), io::Error>;
161+
fn update_memory(&mut self, mem: GM<B>) -> Result<()>;
163162

164163
/// Set handler for communicating with the master by the slave communication channel.
165164
///
@@ -198,7 +197,7 @@ where
198197
evset: EventSet,
199198
vrings: &[V],
200199
thread_id: usize,
201-
) -> result::Result<bool, io::Error>;
200+
) -> Result<bool>;
202201
}
203202

204203
impl<T: VhostUserBackend<V, B>, V, B> VhostUserBackend<V, B> for Arc<T>
@@ -234,11 +233,11 @@ where
234233
self.deref().get_config(offset, size)
235234
}
236235

237-
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<(), io::Error> {
236+
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
238237
self.deref().set_config(offset, buf)
239238
}
240239

241-
fn update_memory(&self, mem: GM<B>) -> Result<(), io::Error> {
240+
fn update_memory(&self, mem: GM<B>) -> Result<()> {
242241
self.deref().update_memory(mem)
243242
}
244243

@@ -260,7 +259,7 @@ where
260259
evset: EventSet,
261260
vrings: &[V],
262261
thread_id: usize,
263-
) -> Result<bool, io::Error> {
262+
) -> Result<bool> {
264263
self.deref()
265264
.handle_event(device_event, evset, vrings, thread_id)
266265
}
@@ -299,11 +298,11 @@ where
299298
self.lock().unwrap().get_config(offset, size)
300299
}
301300

302-
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<(), io::Error> {
301+
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
303302
self.lock().unwrap().set_config(offset, buf)
304303
}
305304

306-
fn update_memory(&self, mem: GM<B>) -> Result<(), io::Error> {
305+
fn update_memory(&self, mem: GM<B>) -> Result<()> {
307306
self.lock().unwrap().update_memory(mem)
308307
}
309308

@@ -325,7 +324,7 @@ where
325324
evset: EventSet,
326325
vrings: &[V],
327326
thread_id: usize,
328-
) -> Result<bool, io::Error> {
327+
) -> Result<bool> {
329328
self.lock()
330329
.unwrap()
331330
.handle_event(device_event, evset, vrings, thread_id)
@@ -365,11 +364,11 @@ where
365364
self.read().unwrap().get_config(offset, size)
366365
}
367366

368-
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<(), io::Error> {
367+
fn set_config(&self, offset: u32, buf: &[u8]) -> Result<()> {
369368
self.write().unwrap().set_config(offset, buf)
370369
}
371370

372-
fn update_memory(&self, mem: GM<B>) -> Result<(), io::Error> {
371+
fn update_memory(&self, mem: GM<B>) -> Result<()> {
373372
self.write().unwrap().update_memory(mem)
374373
}
375374

@@ -391,7 +390,7 @@ where
391390
evset: EventSet,
392391
vrings: &[V],
393392
thread_id: usize,
394-
) -> Result<bool, io::Error> {
393+
) -> Result<bool> {
395394
self.write()
396395
.unwrap()
397396
.handle_event(device_event, evset, vrings, thread_id)
@@ -402,7 +401,6 @@ where
402401
pub mod tests {
403402
use super::*;
404403
use crate::VringRwLock;
405-
use std::io::Error;
406404
use std::sync::Mutex;
407405
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
408406

@@ -454,18 +452,15 @@ pub mod tests {
454452
vec![0xa5u8; 8]
455453
}
456454

457-
fn set_config(&mut self, offset: u32, buf: &[u8]) -> Result<(), Error> {
455+
fn set_config(&mut self, offset: u32, buf: &[u8]) -> Result<()> {
458456
assert_eq!(offset, 0x200);
459457
assert_eq!(buf.len(), 8);
460458
assert_eq!(buf, &[0xa5u8; 8]);
461459

462460
Ok(())
463461
}
464462

465-
fn update_memory(
466-
&mut self,
467-
_atomic_mem: GuestMemoryAtomic<GuestMemoryMmap>,
468-
) -> Result<(), Error> {
463+
fn update_memory(&mut self, _atomic_mem: GuestMemoryAtomic<GuestMemoryMmap>) -> Result<()> {
469464
Ok(())
470465
}
471466

@@ -487,7 +482,7 @@ pub mod tests {
487482
_evset: EventSet,
488483
_vrings: &[VringRwLock],
489484
_thread_id: usize,
490-
) -> Result<bool, Error> {
485+
) -> Result<bool> {
491486
self.events += 1;
492487

493488
Ok(false)

src/event_loop.rs

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
// SPDX-License-Identifier: Apache-2.0
55

66
use std::fmt::{Display, Formatter};
7-
use std::io;
7+
use std::io::{self, Result};
88
use std::marker::PhantomData;
99
use std::os::unix::io::{AsRawFd, RawFd};
10-
use std::result;
1110

1211
use vm_memory::bitmap::Bitmap;
1312
use vmm_sys_util::epoll::{ControlOperation, Epoll, EpollEvent, EventSet};
@@ -127,12 +126,7 @@ where
127126
///
128127
/// When this event is later triggered, the backend implementation of `handle_event` will be
129128
/// called.
130-
pub fn register_listener(
131-
&self,
132-
fd: RawFd,
133-
ev_type: EventSet,
134-
data: u64,
135-
) -> result::Result<(), io::Error> {
129+
pub fn register_listener(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
136130
// `data` range [0...num_queues] is reserved for queues and exit event.
137131
if data <= self.backend.num_queues() as u64 {
138132
Err(io::Error::from_raw_os_error(libc::EINVAL))
@@ -145,12 +139,7 @@ where
145139
///
146140
/// If the event is triggered after this function has been called, the event will be silently
147141
/// dropped.
148-
pub fn unregister_listener(
149-
&self,
150-
fd: RawFd,
151-
ev_type: EventSet,
152-
data: u64,
153-
) -> result::Result<(), io::Error> {
142+
pub fn unregister_listener(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
154143
// `data` range [0...num_queues] is reserved for queues and exit event.
155144
if data <= self.backend.num_queues() as u64 {
156145
Err(io::Error::from_raw_os_error(libc::EINVAL))
@@ -159,22 +148,12 @@ where
159148
}
160149
}
161150

162-
pub(crate) fn register_event(
163-
&self,
164-
fd: RawFd,
165-
ev_type: EventSet,
166-
data: u64,
167-
) -> result::Result<(), io::Error> {
151+
pub(crate) fn register_event(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
168152
self.epoll
169153
.ctl(ControlOperation::Add, fd, EpollEvent::new(ev_type, data))
170154
}
171155

172-
pub(crate) fn unregister_event(
173-
&self,
174-
fd: RawFd,
175-
ev_type: EventSet,
176-
data: u64,
177-
) -> result::Result<(), io::Error> {
156+
pub(crate) fn unregister_event(&self, fd: RawFd, ev_type: EventSet, data: u64) -> Result<()> {
178157
self.epoll
179158
.ctl(ControlOperation::Delete, fd, EpollEvent::new(ev_type, data))
180159
}

src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
extern crate log;
1010

1111
use std::fmt::{Display, Formatter};
12-
use std::io;
13-
use std::result;
1412
use std::sync::{Arc, Mutex};
1513
use std::thread;
1614

@@ -48,7 +46,7 @@ pub enum Error {
4846
/// Failed creating vhost-user slave handler.
4947
CreateSlaveReqHandler(VhostUserError),
5048
/// Failed starting daemon thread.
51-
StartDaemon(io::Error),
49+
StartDaemon(std::io::Error),
5250
/// Failed waiting for daemon thread.
5351
WaitDaemon(std::boxed::Box<dyn std::any::Any + std::marker::Send>),
5452
/// Failed handling a vhost-user request.
@@ -69,7 +67,7 @@ impl Display for Error {
6967
}
7068

7169
/// Result of vhost-user daemon operations.
72-
pub type Result<T> = result::Result<T, Error>;
70+
pub type Result<T> = std::result::Result<T, Error>;
7371

7472
/// Implement a simple framework to run a vhost-user service daemon.
7573
///

0 commit comments

Comments
 (0)