Skip to content

Commit 06ebf77

Browse files
Sudan Landgewearyzen
authored andcommitted
refactor: Improve code readability
refactor code to improve readability and use rust idiomatic methods like avoiding getters. Signed-off-by: Sudan Landge <[email protected]>
1 parent 7420e8b commit 06ebf77

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

src/vmm/src/devices/virtio/virtio_block/io/async_io.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ impl<T: Debug> AsyncFileEngine<T> {
150150
offset,
151151
wrapped_user_data,
152152
))
153-
.map_err(|err_tuple| UserDataError {
154-
user_data: err_tuple.1.user_data,
155-
error: AsyncIoError::IoUring(err_tuple.0),
153+
.map_err(|(io_uring_error, data)| UserDataError {
154+
user_data: data.user_data,
155+
error: AsyncIoError::IoUring(io_uring_error),
156156
})
157157
}
158158

@@ -184,9 +184,9 @@ impl<T: Debug> AsyncFileEngine<T> {
184184
offset,
185185
wrapped_user_data,
186186
))
187-
.map_err(|err_tuple| UserDataError {
188-
user_data: err_tuple.1.user_data,
189-
error: AsyncIoError::IoUring(err_tuple.0),
187+
.map_err(|(io_uring_error, data)| UserDataError {
188+
user_data: data.user_data,
189+
error: AsyncIoError::IoUring(io_uring_error),
190190
})
191191
}
192192

@@ -195,9 +195,9 @@ impl<T: Debug> AsyncFileEngine<T> {
195195

196196
self.ring
197197
.push(Operation::fsync(0, wrapped_user_data))
198-
.map_err(|err_tuple| UserDataError {
199-
user_data: err_tuple.1.user_data,
200-
error: AsyncIoError::IoUring(err_tuple.0),
198+
.map_err(|(io_uring_error, data)| UserDataError {
199+
user_data: data.user_data,
200+
error: AsyncIoError::IoUring(io_uring_error),
201201
})
202202
}
203203

src/vmm/src/io_uring/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ impl<T: Debug> IoUring<T> {
169169
// validate that we actually did register fds
170170
let fd = op.fd();
171171
match self.registered_fds_count {
172-
0 => Err((IoUringError::NoRegisteredFds, op.user_data())),
173-
len if fd >= len => Err((IoUringError::InvalidFixedFd(fd), op.user_data())),
172+
0 => Err((IoUringError::NoRegisteredFds, op.user_data)),
173+
len if fd >= len => Err((IoUringError::InvalidFixedFd(fd), op.user_data)),
174174
_ => {
175175
if self.num_ops >= self.cqueue.count() {
176-
return Err((IoUringError::FullCQueue, op.user_data()));
176+
return Err((IoUringError::FullCQueue, op.user_data));
177177
}
178178
self.squeue
179179
.push(op.into_sqe(&mut self.slab))
@@ -186,14 +186,14 @@ impl<T: Debug> IoUring<T> {
186186
(
187187
IoUringError::SQueue(sqe_err),
188188
// We don't use slab.try_remove here for 2 reasons:
189-
// 1. user_data was insertde in slab with step `op.into_sqe` just
189+
// 1. user_data was inserted in slab with step `op.into_sqe` just
190190
// before the push op so the user_data key should be valid and if
191191
// key is valid then `slab.remove()` will not fail.
192192
// 2. If we use `slab.try_remove()` we'll have to find a way to return
193193
// a default value for the generic type T which is difficult because
194194
// it expands to more crates which don't make it easy to define a
195195
// default/clone type for type T.
196-
// So beleiving that `slab.remove` won't fail we don't use
196+
// So believing that `slab.remove` won't fail we don't use
197197
// the `slab.try_remove` method.
198198
#[allow(clippy::cast_possible_truncation)]
199199
self.slab.remove(user_data_key as usize),

src/vmm/src/io_uring/operation/cqe.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub struct Cqe<T> {
1717
}
1818

1919
impl<T: Debug> Cqe<T> {
20-
/// Construct a Cqe object from a raw `io_uring_cqe`.
21-
pub(crate) fn new(res: i32, user_data: T) -> Self {
20+
/// Construct a Cqe object.
21+
pub fn new(res: i32, user_data: T) -> Self {
2222
Self { res, user_data }
2323
}
2424

src/vmm/src/io_uring/operation/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub struct Operation<T> {
5151
pub(crate) len: Option<u32>,
5252
flags: u8,
5353
pub(crate) offset: Option<u64>,
54-
user_data: T,
54+
pub(crate) user_data: T,
5555
}
5656

5757
// Needed for proptesting.
@@ -117,11 +117,6 @@ impl<T: Debug> Operation<T> {
117117
self.fd
118118
}
119119

120-
/// Consumes the operation and returns the associated `user_data`.
121-
pub fn user_data(self) -> T {
122-
self.user_data
123-
}
124-
125120
// Needed for proptesting.
126121
#[cfg(test)]
127122
pub(crate) fn set_linked(&mut self) {

src/vmm/src/io_uring/operation/sqe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl Sqe {
2424
Self(inner)
2525
}
2626

27-
/// Return the `user_data`.
27+
/// Return the key to the `user_data` stored in slab.
2828
pub(crate) fn user_data(&self) -> u64 {
2929
self.0.user_data
3030
}

0 commit comments

Comments
 (0)