Skip to content

Commit 2dd566b

Browse files
alexandruagjiangliu
authored andcommitted
relax trait bounds for AvailIter and DescriptorChain
We are using a `: GuestAddressSpace` trait bound for `AvailIter` and `DescriptorChain`, but the objects are actually only interested in the fact that `M` can dereference to something that implements `GuestMemory` (which is trivially true for a reference to a `GuestMemory` implementation, for example). Signed-off-by: Alexandru Agache <aagch@amazon.com>
1 parent 9b985ef commit 2dd566b

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

crates/devices/virtio-blk/src/request.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//! approach.
2525
2626
use std::fmt::{self, Display};
27+
use std::ops::Deref;
2728
use std::result;
2829

2930
use crate::defs::{
@@ -32,9 +33,7 @@ use crate::defs::{
3233
};
3334

3435
use virtio_queue::{Descriptor, DescriptorChain};
35-
use vm_memory::{
36-
ByteValued, Bytes, GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryError,
37-
};
36+
use vm_memory::{ByteValued, Bytes, GuestAddress, GuestMemory, GuestMemoryError};
3837

3938
/// Block request parsing errors.
4039
#[derive(Debug)]
@@ -202,7 +201,11 @@ impl Request {
202201
/// # Arguments
203202
/// * `desc_chain` - A mutable reference to the descriptor chain that should point to the
204203
/// buffers of a virtio block request.
205-
pub fn parse<M: GuestAddressSpace>(desc_chain: &mut DescriptorChain<M>) -> Result<Request> {
204+
pub fn parse<M>(desc_chain: &mut DescriptorChain<M>) -> Result<Request>
205+
where
206+
M: Deref,
207+
M::Target: GuestMemory + Sized,
208+
{
206209
let chain_head = desc_chain.next().ok_or(Error::DescriptorChainTooShort)?;
207210
// The head contains the request type which MUST be readable.
208211
if chain_head.is_write_only() {
@@ -235,7 +238,7 @@ impl Request {
235238
}
236239
let status_desc = desc;
237240

238-
Request::check_status_desc::<<M>::M>(desc_chain.memory(), status_desc)?;
241+
Request::check_status_desc(desc_chain.memory(), status_desc)?;
239242

240243
request.status_addr = status_desc.addr();
241244
Ok(request)

crates/virtio-queue/src/lib.rs

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ unsafe impl ByteValued for Descriptor {}
147147

148148
/// A virtio descriptor chain.
149149
#[derive(Clone, Debug)]
150-
pub struct DescriptorChain<M: GuestAddressSpace> {
151-
mem: M::T,
150+
pub struct DescriptorChain<M> {
151+
mem: M,
152152
desc_table: GuestAddress,
153153
queue_size: u16,
154154
head_index: u16,
@@ -157,9 +157,13 @@ pub struct DescriptorChain<M: GuestAddressSpace> {
157157
is_indirect: bool,
158158
}
159159

160-
impl<M: GuestAddressSpace> DescriptorChain<M> {
160+
impl<M> DescriptorChain<M>
161+
where
162+
M: Deref,
163+
M::Target: GuestMemory,
164+
{
161165
fn with_ttl(
162-
mem: M::T,
166+
mem: M,
163167
desc_table: GuestAddress,
164168
queue_size: u16,
165169
ttl: u16,
@@ -177,7 +181,7 @@ impl<M: GuestAddressSpace> DescriptorChain<M> {
177181
}
178182

179183
/// Create a new `DescriptorChain` instance.
180-
fn new(mem: M::T, desc_table: GuestAddress, queue_size: u16, head_index: u16) -> Self {
184+
fn new(mem: M, desc_table: GuestAddress, queue_size: u16, head_index: u16) -> Self {
181185
Self::with_ttl(mem, desc_table, queue_size, queue_size, head_index)
182186
}
183187

@@ -188,8 +192,8 @@ impl<M: GuestAddressSpace> DescriptorChain<M> {
188192

189193
/// Return a `GuestMemory` object that can be used to access the buffers
190194
/// pointed to by the descriptor chain.
191-
pub fn memory(&self) -> &M::M {
192-
&*self.mem
195+
pub fn memory(&self) -> &M::Target {
196+
self.mem.deref()
193197
}
194198

195199
/// Returns an iterator that only yields the readable descriptors in the chain.
@@ -234,7 +238,11 @@ impl<M: GuestAddressSpace> DescriptorChain<M> {
234238
}
235239
}
236240

237-
impl<M: GuestAddressSpace> Iterator for DescriptorChain<M> {
241+
impl<M> Iterator for DescriptorChain<M>
242+
where
243+
M: Deref,
244+
M::Target: GuestMemory + Sized,
245+
{
238246
type Item = Descriptor;
239247

240248
/// Returns the next descriptor in this descriptor chain, if there is one.
@@ -279,12 +287,16 @@ impl<M: GuestAddressSpace> Iterator for DescriptorChain<M> {
279287

280288
/// An iterator for readable or writable descriptors.
281289
#[derive(Clone)]
282-
pub struct DescriptorChainRwIter<M: GuestAddressSpace> {
290+
pub struct DescriptorChainRwIter<M> {
283291
chain: DescriptorChain<M>,
284292
writable: bool,
285293
}
286294

287-
impl<M: GuestAddressSpace> Iterator for DescriptorChainRwIter<M> {
295+
impl<M> Iterator for DescriptorChainRwIter<M>
296+
where
297+
M: Deref,
298+
M::Target: GuestMemory + Sized,
299+
{
288300
type Item = Descriptor;
289301

290302
/// Returns the next descriptor in this descriptor chain, if there is one.
@@ -308,9 +320,9 @@ impl<M: GuestAddressSpace> Iterator for DescriptorChainRwIter<M> {
308320

309321
// We can't derive Debug, because rustc doesn't generate the M::T: Debug
310322
// constraint
311-
impl<M: Debug + GuestAddressSpace> Debug for DescriptorChainRwIter<M>
323+
impl<M> Debug for DescriptorChainRwIter<M>
312324
where
313-
M::T: Debug,
325+
M: Debug,
314326
{
315327
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
316328
f.debug_struct("DescriptorChainRwIter")
@@ -322,16 +334,16 @@ where
322334

323335
/// Consuming iterator over all available descriptor chain heads in the queue.
324336
#[derive(Debug)]
325-
pub struct AvailIter<'b, M: GuestAddressSpace> {
326-
mem: M::T,
337+
pub struct AvailIter<'b, M> {
338+
mem: M,
327339
desc_table: GuestAddress,
328340
avail_ring: GuestAddress,
329341
last_index: Wrapping<u16>,
330342
queue_size: u16,
331343
next_avail: &'b mut Wrapping<u16>,
332344
}
333345

334-
impl<'b, M: GuestAddressSpace> AvailIter<'b, M> {
346+
impl<'b, M> AvailIter<'b, M> {
335347
/// Goes back one position in the available descriptor chain offered by the driver.
336348
///
337349
/// Rust does not support bidirectional iterators. This is the only way to revert the effect
@@ -344,7 +356,11 @@ impl<'b, M: GuestAddressSpace> AvailIter<'b, M> {
344356
}
345357
}
346358

347-
impl<'b, M: GuestAddressSpace> Iterator for AvailIter<'b, M> {
359+
impl<'b, M> Iterator for AvailIter<'b, M>
360+
where
361+
M: Clone + Deref,
362+
M::Target: GuestMemory + Sized,
363+
{
348364
type Item = DescriptorChain<M>;
349365

350366
fn next(&mut self) -> Option<Self::Item> {
@@ -559,7 +575,7 @@ pub struct QueueState<M: GuestAddressSpace> {
559575

560576
impl<M: GuestAddressSpace> QueueState<M> {
561577
/// Get a consuming iterator over all available descriptor chain heads offered by the driver.
562-
pub fn iter(&mut self, mem: M::T) -> Result<AvailIter<'_, M>, Error> {
578+
pub fn iter(&mut self, mem: M::T) -> Result<AvailIter<'_, M::T>, Error> {
563579
self.avail_idx(&mem, Ordering::Acquire)
564580
.map(move |idx| AvailIter {
565581
mem,
@@ -1106,7 +1122,7 @@ impl<M: GuestAddressSpace, S: QueueStateT<M>> Queue<M, S> {
11061122

11071123
impl<M: GuestAddressSpace> Queue<M, QueueState<M>> {
11081124
/// A consuming iterator over all available descriptor chain heads offered by the driver.
1109-
pub fn iter(&mut self) -> Result<AvailIter<'_, M>, Error> {
1125+
pub fn iter(&mut self) -> Result<AvailIter<'_, M::T>, Error> {
11101126
self.state.iter(self.mem.memory())
11111127
}
11121128
}

0 commit comments

Comments
 (0)