From 103d900690df4a0bd752d273b280238e5761bc60 Mon Sep 17 00:00:00 2001 From: Chuanhong Guo Date: Mon, 26 Feb 2024 15:14:29 +0800 Subject: [PATCH] fix clippy errors Signed-off-by: Chuanhong Guo --- src/bl2.rs | 28 +++++++++++----------------- src/bootrom.rs | 24 +++++++++--------------- src/main.rs | 6 +++--- 3 files changed, 23 insertions(+), 35 deletions(-) diff --git a/src/bl2.rs b/src/bl2.rs index 89f372d..f008ec8 100644 --- a/src/bl2.rs +++ b/src/bl2.rs @@ -3,8 +3,8 @@ use std::slice; use std::time::Duration; use serialport::{ClearBuffer, SerialPort}; -static BL2_HANDSHAKE_REQ: &'static [u8] = "mudl".as_bytes(); -static BL2_HANDSHAKE_RESP: &'static [u8] = "TF-A".as_bytes(); +static BL2_HANDSHAKE_REQ: &[u8] = "mudl".as_bytes(); +static BL2_HANDSHAKE_RESP: &[u8] = "TF-A".as_bytes(); pub struct BL2 { port: Box, @@ -26,10 +26,10 @@ impl BL2 { let mut rx_char = 0; self.port.set_timeout(Duration::from_millis(500)).unwrap(); while i < 4 { - self.port.write(&BL2_HANDSHAKE_REQ[i..i + 1]) + self.port.write_all(&BL2_HANDSHAKE_REQ[i..i + 1]) .expect("failed to write to port."); - if let Ok(len) = self.port.read(slice::from_mut(&mut rx_char)) { - if len == 1 && BL2_HANDSHAKE_RESP[i] == rx_char { + if let Ok(()) = self.port.read_exact(slice::from_mut(&mut rx_char)) { + if BL2_HANDSHAKE_RESP[i] == rx_char { i += 1; } } @@ -41,8 +41,8 @@ impl BL2 { fn echo(&mut self, buf: &[u8]) { let mut rx_buf: Vec = vec![0; buf.len()]; self.port.set_timeout(Duration::from_millis(100)).unwrap(); - self.port.write(buf).expect("failed to write to port."); - self.port.read(rx_buf.as_mut_slice()).unwrap(); + self.port.write_all(buf).expect("failed to write to port."); + self.port.read_exact(rx_buf.as_mut_slice()).unwrap(); if buf != rx_buf { panic!("returned data isn't the same. Tx: {:?} Rx: {:?}", buf, rx_buf); } @@ -50,26 +50,20 @@ impl BL2 { fn read_be16(&mut self) -> u16 { let mut rx_buf: Vec = vec![0; 2]; - let len = self.port.read(rx_buf.as_mut_slice()).unwrap(); - if len != 2 { - panic!("not enough data returned.") - } + self.port.read_exact(rx_buf.as_mut_slice()).unwrap(); u16::from_be_bytes(rx_buf.try_into().unwrap()) } fn read_be32(&mut self) -> u32 { let mut rx_buf: Vec = vec![0; 4]; - let len = self.port.read(rx_buf.as_mut_slice()).unwrap(); - if len != 4 { - panic!("not enough data returned.") - } + self.port.read_exact(rx_buf.as_mut_slice()).unwrap(); u32::from_be_bytes(rx_buf.try_into().unwrap()) } pub fn version(&mut self) -> u8 { self.echo(slice::from_ref(&1)); let mut rx_char = 0; - self.port.read(slice::from_mut(&mut rx_char)).unwrap(); + self.port.read_exact(slice::from_mut(&mut rx_char)).unwrap(); rx_char } @@ -104,7 +98,7 @@ impl BL2 { self.echo(&u16::to_be_bytes(fip.len() as u16)); let checksum = BL2::fip_packet_checksum(fip); self.echo(&u16::to_be_bytes(checksum)); - self.port.write(fip).expect("failed to send fip packet."); + self.port.write_all(fip).expect("failed to send fip packet."); while self.port.bytes_to_write().unwrap() > 0 { std::thread::sleep(Duration::from_millis(50)); diff --git a/src/bootrom.rs b/src/bootrom.rs index 7f5f47c..aa1e4b3 100644 --- a/src/bootrom.rs +++ b/src/bootrom.rs @@ -3,7 +3,7 @@ use std::slice; use std::time::Duration; use serialport::{ClearBuffer, SerialPort}; -static BROM_HANDSHAKE: &'static [u8] = &[0xa0, 0x0a, 0x50, 0x05]; +static BROM_HANDSHAKE: &[u8] = &[0xa0, 0x0a, 0x50, 0x05]; pub struct BootROM { port: Box, } @@ -24,10 +24,10 @@ impl BootROM { let mut rx_char = 0; self.port.set_timeout(Duration::from_millis(5)).unwrap(); while i < BROM_HANDSHAKE.len() { - self.port.write(&BROM_HANDSHAKE[i .. i+1]) + self.port.write_all(&BROM_HANDSHAKE[i .. i+1]) .expect("failed to write to port."); - if let Ok(len) = self.port.read(slice::from_mut(&mut rx_char)) { - if len == 1 && BROM_HANDSHAKE[i] == !rx_char { + if let Ok(()) = self.port.read_exact(slice::from_mut(&mut rx_char)) { + if BROM_HANDSHAKE[i] == !rx_char { i += 1; } } @@ -39,8 +39,8 @@ impl BootROM { fn echo(&mut self, buf: &[u8]) { let mut rx_buf: Vec =vec![0; buf.len()]; self.port.set_timeout(Duration::from_millis(100)).unwrap(); - self.port.write(buf).expect("failed to write to port."); - self.port.read(rx_buf.as_mut_slice()).unwrap(); + self.port.write_all(buf).expect("failed to write to port."); + self.port.read_exact(rx_buf.as_mut_slice()).unwrap(); if buf != rx_buf { panic!("returned data isn't the same. Tx: {:?} Rx: {:?}", buf, rx_buf); } @@ -48,19 +48,13 @@ impl BootROM { fn read_be16(&mut self) -> u16 { let mut rx_buf: Vec = vec![0; 2]; - let len = self.port.read(rx_buf.as_mut_slice()).unwrap(); - if len != 2 { - panic!("not enough data returned.") - } + self.port.read_exact(rx_buf.as_mut_slice()).unwrap(); u16::from_be_bytes(rx_buf.try_into().unwrap()) } fn read_be32(&mut self) -> u32 { let mut rx_buf: Vec = vec![0; 4]; - let len = self.port.read(rx_buf.as_mut_slice()).unwrap(); - if len != 4 { - panic!("not enough data returned.") - } + self.port.read_exact(rx_buf.as_mut_slice()).unwrap(); u32::from_be_bytes(rx_buf.try_into().unwrap()) } @@ -110,7 +104,7 @@ impl BootROM { panic!("send_da cmd status: {}", ret); } - self.port.write(da_buf).unwrap(); + self.port.write_all(da_buf).unwrap(); while self.port.bytes_to_write().unwrap() > 0 { std::thread::sleep(Duration::from_millis(200)); } diff --git a/src/main.rs b/src/main.rs index 5ecb21e..7a0ce14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -133,14 +133,14 @@ fn open_serial(port: Option<&str>) -> Box { Some(p) => p.to_owned(), None => { let ports = serialport::available_ports().expect("No serial ports found."); - ports.into_iter().nth(0).expect("No serial ports found.").port_name + ports.into_iter().next().expect("No serial ports found.").port_name }, }; println!("Using serial port: {}", port); - return serialport::new(&port, 115200) + serialport::new(&port, 115200) .timeout(Duration::from_secs(2)) - .open().expect("Failed to open port"); + .open().expect("Failed to open port") } fn main() {