From 399a656c82fde2e46faaf487eef76244fc35ffe8 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Fri, 20 Sep 2024 09:55:30 +0300 Subject: [PATCH] template: add and fix workspace lints No functional changes. Signed-off-by: Manos Pitsidianakis --- vhost-device-template/Cargo.toml | 3 ++ vhost-device-template/src/main.rs | 10 +++---- vhost-device-template/src/vhu_foo.rs | 42 +++++++++++++--------------- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/vhost-device-template/Cargo.toml b/vhost-device-template/Cargo.toml index 0632c9bf3..a40d2532e 100644 --- a/vhost-device-template/Cargo.toml +++ b/vhost-device-template/Cargo.toml @@ -12,6 +12,9 @@ publish = false # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lints] +workspace = true + [features] xen = ["vm-memory/xen", "vhost/xen", "vhost-user-backend/xen"] diff --git a/vhost-device-template/src/main.rs b/vhost-device-template/src/main.rs index 349d3671c..a229a6061 100644 --- a/vhost-device-template/src/main.rs +++ b/vhost-device-template/src/main.rs @@ -53,7 +53,7 @@ impl TryFrom for FooConfiguration { // Even though this try_from() conversion always succeeds, in cases where the device's // configuration type needs to validate arguments and/or make operations that can fail a // TryFrom<_> implementation will be necessary. - Ok(FooConfiguration { + Ok(Self { socket_path: args.socket_path, }) } @@ -65,7 +65,7 @@ pub(crate) struct FooInfo { } impl FooInfo { - pub fn new() -> Self { + pub const fn new() -> Self { Self { counter: 0 } } @@ -78,7 +78,7 @@ impl FooInfo { fn start_backend(args: FooArgs) -> Result<()> { let config = FooConfiguration::try_from(args).unwrap(); - let socket_path = config.socket_path.clone(); + let socket_path = config.socket_path; let info = FooInfo::new(); let handle: JoinHandle> = spawn(move || loop { @@ -119,8 +119,8 @@ mod tests { use super::*; impl FooArgs { - pub(crate) fn from_args(path: &Path) -> FooArgs { - FooArgs { + pub(crate) fn from_args(path: &Path) -> Self { + Self { socket_path: path.to_path_buf(), } } diff --git a/vhost-device-template/src/vhu_foo.rs b/vhost-device-template/src/vhu_foo.rs index 4bf52f392..1c9527be3 100644 --- a/vhost-device-template/src/vhu_foo.rs +++ b/vhost-device-template/src/vhu_foo.rs @@ -32,7 +32,7 @@ type Result = std::result::Result; #[derive(Copy, Clone, Debug, Eq, PartialEq, ThisError)] /// Errors related to vhost-device-foo daemon. -pub(crate) enum Error { +pub enum Error { #[error("Failed to handle event, didn't match EPOLLIN")] HandleEventNotEpollIn, #[error("Failed to handle unknown event")] @@ -47,11 +47,11 @@ pub(crate) enum Error { impl convert::From for io::Error { fn from(e: Error) -> Self { - io::Error::new(io::ErrorKind::Other, e) + Self::new(io::ErrorKind::Other, e) } } -pub(crate) struct VhostUserFooBackend { +pub struct VhostUserFooBackend { info: FooInfo, event_idx: bool, pub exit_event: EventFd, @@ -62,7 +62,7 @@ type FooDescriptorChain = DescriptorChain Result { - Ok(VhostUserFooBackend { + Ok(Self { info, event_idx: false, exit_event: EventFd::new(EFD_NONBLOCK).map_err(|_| Error::EventFdFailed)?, @@ -85,7 +85,7 @@ impl VhostUserFooBackend { // // The layout of the various structures, to be read from and written into the descriptor // buffers, is defined in the Virtio specification for each protocol. - for desc_chain in requests.clone() { + for desc_chain in requests { let counter = self.info.counter(); let descriptors: Vec<_> = desc_chain.clone().collect(); @@ -266,7 +266,7 @@ mod tests { fn prepare_descriptors( mut next_addr: u64, mem: &GuestMemoryLoadGuard>, - buf: &mut Vec, + buf: &[u8], ) -> Vec { let mut descriptors = Vec::new(); let mut index = 0; @@ -284,7 +284,7 @@ mod tests { VRING_DESC_F_NEXT as u16, index + 1, ); - next_addr += desc_out.len() as u64; + next_addr += u64::from(desc_out.len()); index += 1; mem.write_obj::(out_hdr, desc_out.addr()) @@ -299,7 +299,7 @@ mod tests { (VRING_DESC_F_WRITE | VRING_DESC_F_NEXT) as u16, index + 1, ); - next_addr += desc_buf.len() as u64; + next_addr += u64::from(desc_buf.len()); mem.write(buf, desc_buf.addr()).unwrap(); descriptors.push(desc_buf); @@ -317,7 +317,7 @@ mod tests { } // Prepares a single chain of descriptors - fn prepare_desc_chain(buf: &mut Vec) -> (VhostUserFooBackend, VringRwLock) { + fn prepare_desc_chain(buf: &[u8]) -> (VhostUserFooBackend, VringRwLock) { let (mut backend, mem, vring) = init(); let mem_handle = mem.memory(); let vq = MockSplitQueue::new(&*mem_handle, 16); @@ -351,7 +351,7 @@ mod tests { // Prepares a chain of descriptors fn prepare_desc_chains( mem: &GuestMemoryAtomic, - buf: &mut Vec, + buf: &[u8], ) -> FooDescriptorChain { let mem_handle = mem.memory(); let vq = MockSplitQueue::new(&*mem_handle, 16); @@ -395,8 +395,8 @@ mod tests { #[test] fn process_request_single() { // Single valid descriptor - let mut buf: Vec = vec![0; 30]; - let (mut backend, vring) = prepare_desc_chain(&mut buf); + let buf: Vec = vec![0; 30]; + let (mut backend, vring) = prepare_desc_chain(&buf); backend.process_queue(&vring).unwrap(); } @@ -405,19 +405,17 @@ mod tests { // Multiple valid descriptors let (mut backend, mem, vring) = init(); - let mut bufs: Vec> = vec![vec![0; 30]; 6]; + let bufs: Vec> = vec![vec![0; 30]; 6]; let desc_chains = vec![ - prepare_desc_chains(&mem, &mut bufs[0]), - prepare_desc_chains(&mem, &mut bufs[1]), - prepare_desc_chains(&mem, &mut bufs[2]), - prepare_desc_chains(&mem, &mut bufs[3]), - prepare_desc_chains(&mem, &mut bufs[4]), - prepare_desc_chains(&mem, &mut bufs[5]), + prepare_desc_chains(&mem, &bufs[0]), + prepare_desc_chains(&mem, &bufs[1]), + prepare_desc_chains(&mem, &bufs[2]), + prepare_desc_chains(&mem, &bufs[3]), + prepare_desc_chains(&mem, &bufs[4]), + prepare_desc_chains(&mem, &bufs[5]), ]; - backend - .process_requests(desc_chains.clone(), &vring) - .unwrap(); + backend.process_requests(desc_chains, &vring).unwrap(); } #[test]