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

Commit 72d754b

Browse files
committed
Simplify trait/impl bound declarations
Currently trait bound declarations is a little over complex when defines traits or implements traits/structs. Let's simplfy it. Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
1 parent eb393cf commit 72d754b

File tree

4 files changed

+33
-48
lines changed

4 files changed

+33
-48
lines changed

src/backend.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ use super::GM;
3333

3434
/// Trait with interior mutability for vhost user backend servers to implement concrete services.
3535
///
36-
/// To support multi-threading and asynchronous IO, we enforce `the Send + Sync + 'static`.
37-
/// So there's no plan for support of "Rc<T>" and "RefCell<T>".
38-
pub trait VhostUserBackend<V, B = ()>: Send + Sync + 'static
36+
/// To support multi-threading and asynchronous IO, we enforce `Send + Sync` bound.
37+
pub trait VhostUserBackend<V, B = ()>: Send + Sync
3938
where
4039
V: VringT<GM<B>>,
4140
B: Bitmap + 'static,
@@ -118,7 +117,7 @@ where
118117
}
119118

120119
/// Trait without interior mutability for vhost user backend servers to implement concrete services.
121-
pub trait VhostUserBackendMut<V, B = ()>: Send + Sync + 'static
120+
pub trait VhostUserBackendMut<V, B = ()>: Send + Sync
122121
where
123122
V: VringT<GM<B>>,
124123
B: Bitmap + 'static,

src/event_loop.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,7 @@ pub type VringEpollResult<T> = std::result::Result<T, VringEpollError>;
5858
/// - add file descriptors to be monitored by the epoll fd
5959
/// - remove registered file descriptors from the epoll fd
6060
/// - run the event loop to handle pending events on the epoll fd
61-
pub struct VringEpollHandler<S, V, B>
62-
where
63-
S: VhostUserBackend<V, B>,
64-
V: VringT<GM<B>>,
65-
B: Bitmap + 'static,
66-
{
61+
pub struct VringEpollHandler<S, V, B> {
6762
epoll: Epoll,
6863
backend: S,
6964
vrings: Vec<V>,
@@ -72,6 +67,15 @@ where
7267
phantom: PhantomData<B>,
7368
}
7469

70+
impl<S, V, B> VringEpollHandler<S, V, B> {
71+
/// Send `exit event` to break the event loop.
72+
pub fn send_exit_event(&self) {
73+
if let Some(eventfd) = self.exit_event_fd.as_ref() {
74+
let _ = eventfd.write(1);
75+
}
76+
}
77+
}
78+
7579
impl<S, V, B> VringEpollHandler<S, V, B>
7680
where
7781
S: VhostUserBackend<V, B>,
@@ -115,13 +119,6 @@ where
115119
Ok(handler)
116120
}
117121

118-
/// Send `exit event` to break the event loop.
119-
pub fn send_exit_event(&self) {
120-
if let Some(eventfd) = self.exit_event_fd.as_ref() {
121-
let _ = eventfd.write(1);
122-
}
123-
}
124-
125122
/// Register an event into the epoll fd.
126123
///
127124
/// When this event is later triggered, the backend implementation of `handle_event` will be

src/handler.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,7 @@ struct AddrMapping {
7171
gpa_base: u64,
7272
}
7373

74-
pub struct VhostUserHandler<S, V, B>
75-
where
76-
S: VhostUserBackend<V, B>,
77-
V: VringT<GM<B>>,
78-
B: Bitmap + 'static,
79-
{
74+
pub struct VhostUserHandler<S, V, B: Bitmap + 'static> {
8075
backend: S,
8176
handlers: Vec<Arc<VringEpollHandler<S, V, B>>>,
8277
owned: bool,
@@ -92,11 +87,12 @@ where
9287
worker_threads: Vec<thread::JoinHandle<VringEpollResult<()>>>,
9388
}
9489

90+
// Ensure VhostUserHandler: Clone + Send + Sync + 'static.
9591
impl<S, V, B> VhostUserHandler<S, V, B>
9692
where
97-
S: VhostUserBackend<V, B> + Clone,
93+
S: VhostUserBackend<V, B> + Clone + 'static,
9894
V: VringT<GM<B>> + Clone + Send + Sync + 'static,
99-
B: Bitmap + Clone + Send + Sync,
95+
B: Bitmap + Clone + Send + Sync + 'static,
10096
{
10197
pub(crate) fn new(backend: S, atomic_mem: GM<B>) -> VhostUserHandlerResult<Self> {
10298
let num_queues = backend.num_queues();
@@ -151,16 +147,7 @@ where
151147
}
152148
}
153149

154-
impl<S, V, B> VhostUserHandler<S, V, B>
155-
where
156-
S: VhostUserBackend<V, B>,
157-
V: VringT<GM<B>>,
158-
B: Bitmap,
159-
{
160-
pub(crate) fn get_epoll_handlers(&self) -> Vec<Arc<VringEpollHandler<S, V, B>>> {
161-
self.handlers.clone()
162-
}
163-
150+
impl<S, V, B: Bitmap> VhostUserHandler<S, V, B> {
164151
pub(crate) fn send_exit_event(&self) {
165152
for handler in self.handlers.iter() {
166153
handler.send_exit_event();
@@ -176,6 +163,17 @@ where
176163

177164
Err(VhostUserHandlerError::MissingMemoryMapping)
178165
}
166+
}
167+
168+
impl<S, V, B> VhostUserHandler<S, V, B>
169+
where
170+
S: VhostUserBackend<V, B>,
171+
V: VringT<GM<B>>,
172+
B: Bitmap,
173+
{
174+
pub(crate) fn get_epoll_handlers(&self) -> Vec<Arc<VringEpollHandler<S, V, B>>> {
175+
self.handlers.clone()
176+
}
179177

180178
fn vring_needs_init(&self, vring: &V) -> bool {
181179
let vring_state = vring.get_ref();
@@ -581,12 +579,7 @@ where
581579
}
582580
}
583581

584-
impl<S, V, B> Drop for VhostUserHandler<S, V, B>
585-
where
586-
S: VhostUserBackend<V, B>,
587-
V: VringT<GM<B>>,
588-
B: Bitmap,
589-
{
582+
impl<S, V, B: Bitmap> Drop for VhostUserHandler<S, V, B> {
590583
fn drop(&mut self) {
591584
// Signal all working threads to exit.
592585
self.send_exit_event();

src/lib.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,15 @@ pub type Result<T> = std::result::Result<T, Error>;
7373
///
7474
/// This structure is the public API the backend is allowed to interact with in order to run
7575
/// a fully functional vhost-user daemon.
76-
pub struct VhostUserDaemon<S, V, B = ()>
77-
where
78-
S: VhostUserBackend<V, B>,
79-
V: VringT<GM<B>> + Clone + Send + Sync + 'static,
80-
B: Bitmap + 'static,
81-
{
76+
pub struct VhostUserDaemon<S, V, B: Bitmap + 'static = ()> {
8277
name: String,
8378
handler: Arc<Mutex<VhostUserHandler<S, V, B>>>,
8479
main_thread: Option<thread::JoinHandle<Result<()>>>,
8580
}
8681

8782
impl<S, V, B> VhostUserDaemon<S, V, B>
8883
where
89-
S: VhostUserBackend<V, B> + Clone,
84+
S: VhostUserBackend<V, B> + Clone + 'static,
9085
V: VringT<GM<B>> + Clone + Send + Sync + 'static,
9186
B: NewBitmap + Clone + Send + Sync,
9287
{
@@ -167,6 +162,7 @@ where
167162
/// This is necessary to perform further actions like registering and unregistering some extra
168163
/// event file descriptors.
169164
pub fn get_epoll_handlers(&self) -> Vec<Arc<VringEpollHandler<S, V, B>>> {
165+
// Do not expect poisoned lock.
170166
self.handler.lock().unwrap().get_epoll_handlers()
171167
}
172168
}

0 commit comments

Comments
 (0)