From 5f5e3bcd18040ae3c9bf23196961b804ac8831a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20van=20Dorst?= Date: Mon, 21 Aug 2023 20:58:26 +0200 Subject: [PATCH] Replace size_align_u32() with next_multiple_of() Currently next_multiple_of() is behinged a Feature gate: int_rounding. See https://github.com/rust-lang/rust/issues/88581 But it seems that this function is stablized in rust 1.73. See https://github.com/rust-lang/rust/pull/94455 Currently Embassy is still using nightly for many other unstable features. So I do see an issue to use this function. --- embassy-net-adin1110/src/lib.rs | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/embassy-net-adin1110/src/lib.rs b/embassy-net-adin1110/src/lib.rs index e917edcc8c..8b81065c51 100644 --- a/embassy-net-adin1110/src/lib.rs +++ b/embassy-net-adin1110/src/lib.rs @@ -95,11 +95,6 @@ pub struct ADIN1110 { crc: bool, } -/// Round size up the N u32; -pub(crate) fn size_align_u32(size: u32) -> u32 { - (size + 3) & 0xFFFF_FFFC -} - impl ADIN1110 { pub fn new(spi: SPI, crc: bool) -> Self { Self { spi, crc } @@ -192,14 +187,12 @@ impl ADIN1110 { let mut tx_buf = Vec::::new(); // Size of the frame, also includes the appednded header. - let packet_size = self.read_reg(sr::RX_FSIZE).await?; + let packet_size = self.read_reg(sr::RX_FSIZE).await? as usize; // Packet read of write to the MAC packet buffer must be a multipul of 4! - let read_size = size_align_u32(packet_size); + let read_size = packet_size.next_multiple_of(4); - if packet_size < u32::try_from(FRAME_HEADER_LEN + FSC_LEN).unwrap() - || read_size > u32::try_from(packet.len()).unwrap() - { + if packet_size < (FRAME_HEADER_LEN + FSC_LEN) || read_size > packet.len() { return Err(AdinError::PACKET_TOO_BIG); } @@ -836,18 +829,6 @@ mod tests { spi.done(); } - #[test] - fn align_size() { - assert_eq!(size_align_u32(1), 4); - assert_eq!(size_align_u32(2), 4); - assert_eq!(size_align_u32(3), 4); - assert_eq!(size_align_u32(4), 4); - assert_eq!(size_align_u32(5), 8); - assert_eq!(size_align_u32(6), 8); - assert_eq!(size_align_u32(7), 8); - assert_eq!(size_align_u32(8), 8); - } - // #[test] // fn write_packet_to_fifo_less_64b_with_crc() { // // Configure expectations @@ -1224,10 +1205,9 @@ mod tests { // Packet FCS spi_packet.extend_from_slice(&[147, 149, 213, 68]).unwrap(); - let spi_packet_len = u32::try_from(spi_packet.len()).unwrap(); - // SPI HEADER Padding of u32 - for _ in spi_packet_len..size_align_u32(spi_packet_len) { + let spi_packet_len = spi_packet.len(); + for _ in spi_packet_len..spi_packet_len.next_multiple_of(4) { spi_packet.push(0x00).unwrap(); }