Skip to content

Commit 18980df

Browse files
committed
mock: Add typed accessors
adds typed accessor methods for ring fields for endianness conversion Signed-off-by: Nelson Wong <[email protected]>
1 parent 4904d3c commit 18980df

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

virtio-queue/src/mock.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,51 @@ impl<'a, M: GuestMemory, T: RingAccess> SplitQueueRing<'a, M, T> {
192192
.checked_add(self.ring.len as GuestUsize)
193193
.unwrap()
194194
}
195+
196+
/// Load the value of the `flags` field.
197+
pub fn load_flags(&self) -> u16 {
198+
u16::from_le(self.flags.load())
199+
}
200+
201+
/// Store the `flags` field.
202+
pub fn store_flags(&self, val: u16) {
203+
self.flags.store(u16::to_le(val))
204+
}
205+
206+
/// Load the value of the `idx` field.
207+
pub fn load_idx(&self) -> u16 {
208+
u16::from_le(self.idx.load())
209+
}
210+
211+
/// Store the `idx` field.
212+
pub fn store_idx(&self, val: u16) {
213+
self.idx.store(u16::to_le(val))
214+
}
215+
216+
/// Load a ring entry at `index`.
217+
pub fn load_ring_entry(&self, index: usize) -> Result<T, MockError> {
218+
self.ring
219+
.ref_at(index)
220+
.map(|r| T::from_le(r.load()))
221+
}
222+
223+
/// Store a ring entry at `index`.
224+
pub fn store_ring_entry(&self, index: usize, val: T) -> Result<(), MockError> {
225+
self.ring
226+
.ref_at(index)
227+
.map(|r| r.store(val.to_le()))
228+
}
195229

196-
/// Return a reference to the idx field.
197-
pub fn idx(&self) -> &Ref<'a, M, u16> {
198-
&self.idx
230+
/// Load the value of the event field.
231+
pub fn load_event(&self) -> u16 {
232+
u16::from_le(self.event.load())
199233
}
200234

201-
/// Return a reference to the ring field.
202-
pub fn ring(&self) -> &ArrayRef<'a, M, T> {
203-
&self.ring
235+
/// Store the event field.
236+
pub fn store_event(&self, val: u16) {
237+
self.event.store(u16::to_le(val))
204238
}
239+
205240
}
206241

207242
/// The available ring is used by the driver to offer buffers to the device.

0 commit comments

Comments
 (0)