From f9da8bc4ffabfcb6d17dc02bddb958ddd15df39d Mon Sep 17 00:00:00 2001 From: Kalle Wachsmuth Date: Wed, 4 Dec 2024 19:20:44 +0100 Subject: [PATCH] use rust 1.82.0 features --- Cargo.toml | 3 +-- deforest_derive/Cargo.toml | 2 +- deforest_derive/lib.rs | 4 ++-- src/alloc.rs | 10 +++------- src/blob.rs | 7 +++---- src/blob/node.rs | 3 ++- src/blob/token.rs | 9 ++++----- src/lib.rs | 22 ++++++---------------- src/prop_value.rs | 9 +++++++-- 9 files changed, 29 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3374c79..7ca0028 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ unescaped_backticks = "warn" name = "deforest" version = "0.3.2" edition = "2021" -rust-version = "1.74" +rust-version = "1.82.0" description = "efficient `#![no_std]` parser for devicetree blobs" repository = "https://github.com/kadiwa4/deforest" license = "MIT" @@ -56,7 +56,6 @@ zerocopy = { version = "0.8.7", features = ["derive"] } [features] alloc = ["fallible-iterator/alloc"] -std = ["alloc", "fallible-iterator/std"] derive = ["deforest_derive"] model = [] diff --git a/deforest_derive/Cargo.toml b/deforest_derive/Cargo.toml index 70099b5..5ee8113 100644 --- a/deforest_derive/Cargo.toml +++ b/deforest_derive/Cargo.toml @@ -2,7 +2,7 @@ name = "deforest_derive" version = "0.3.0" edition = "2021" -rust-version = "1.70" +rust-version = "1.82.0" description = "derive macros for the crate deforest" readme = "../README.md" repository = "https://github.com/kadiwa4/deforest" diff --git a/deforest_derive/lib.rs b/deforest_derive/lib.rs index 2fdb0d3..5b937b4 100644 --- a/deforest_derive/lib.rs +++ b/deforest_derive/lib.rs @@ -328,10 +328,10 @@ Valid forms are: "children" => match meta { Meta::Path(_) => Some((ItemKind::Children, None)), Meta::List(list) => { - if !meta_list_get_single(&list) + if meta_list_get_single(&list) .as_ref() .and_then(|i| i.path().get_ident()) - .is_some_and(|i| *i == "rest") + .is_none_or(|i| *i != "rest") { panic_invalid(); } diff --git a/src/alloc.rs b/src/alloc.rs index 1e1e779..cd13e9e 100644 --- a/src/alloc.rs +++ b/src/alloc.rs @@ -1,10 +1,6 @@ //! Features depending on memory allocation. -use core::{ - iter::Map, - mem::{size_of, size_of_val}, - slice, -}; +use core::{iter::Map, slice}; use std_alloc::{boxed::Box, string::String, vec::Vec}; use fallible_iterator::FallibleIterator; @@ -84,14 +80,14 @@ impl<'a> DevicetreeBuilder<'a> { } )); blob.extend(self.mem_reserve_entries.iter().flat_map( - |e| -> [u64; blob::RawReserveEntry::FIELD_COUNT] { + |e| -> [u64; blob::RawReserveEntry::NUM_FIELDS] { zerocopy::transmute!(blob::RawReserveEntry { address: e.address.to_be(), size: e.size.to_be(), }) }, )); - blob.extend([0; blob::RawReserveEntry::FIELD_COUNT]); + blob.extend([0; blob::RawReserveEntry::NUM_FIELDS]); // SAFETY: after the requested buffer is filled with data, len can be set to the capacity. // the constructed Devicetree would pass all of the checks, so we can skip them diff --git a/src/blob.rs b/src/blob.rs index 76d6138..433b1ef 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -9,7 +9,6 @@ mod token; use core::{ fmt::{self, Debug, Display, Formatter, Write}, - mem::{size_of, size_of_val}, ptr::NonNull, slice, }; @@ -253,10 +252,10 @@ impl Devicetree { return Err(BlobError::InvalidBlockOrder); } - if !usize::try_from(u32::from_be(self.header().size_dt_strings)) + if usize::try_from(u32::from_be(self.header().size_dt_strings)) .ok() .and_then(|s| usize::checked_add(strings_offset, s)) - .is_some_and(|e| e <= exact_size) + .is_none_or(|e| e > exact_size) { return Err(BlobError::BlockOutOfBounds); } @@ -584,5 +583,5 @@ pub(crate) struct RawReserveEntry { } impl RawReserveEntry { - pub(crate) const FIELD_COUNT: usize = size_of::() / DTB_OPTIMAL_ALIGN; + pub(crate) const NUM_FIELDS: usize = size_of::() / DTB_OPTIMAL_ALIGN; } diff --git a/src/blob/node.rs b/src/blob/node.rs index f40e0c8..6b045f7 100644 --- a/src/blob/node.rs +++ b/src/blob/node.rs @@ -143,7 +143,8 @@ impl<'dtb> Node<'dtb> { pub fn get_children<'n>( &self, name: &'n str, - ) -> fallible_iterator::Filter, impl FnMut(&Node<'dtb>) -> Result + 'n> { + ) -> fallible_iterator::Filter, impl FnMut(&Self) -> Result + use<'dtb, 'n>> + { Children(Items::new(self, self.contents)) .filter(move |n| n.split_name().map(|(n, _)| n == name)) } diff --git a/src/blob/token.rs b/src/blob/token.rs index bed2d65..5cb974b 100644 --- a/src/blob/token.rs +++ b/src/blob/token.rs @@ -1,7 +1,6 @@ use core::{ cmp::Ordering, hash::{Hash, Hasher}, - mem::size_of, }; use zerocopy::FromBytes; @@ -167,7 +166,8 @@ impl Devicetree { let len = DtUint::try_from(u32::from_be(header.len)) .map_err(|_| BlobError::InvalidPropertyHeader)?; - let value = util::slice_get_with_len(blob, offset, len as usize) + let value = blob + .get(offset..offset + len as usize) .ok_or(BlobError::InvalidPropertyHeader)?; cursor.increase_offset(len, blob)?; @@ -196,13 +196,12 @@ fn next_raw(blob: &[u8], cursor: &mut Cursor) -> Result> { const END: u32 = RawToken::End as u32; let offset = cursor.offset as usize; - let Some(token) = util::slice_get_with_len(blob, offset, TOKEN_SIZE as usize) else { + let Some(&token) = blob[offset..].first_chunk::<{ TOKEN_SIZE as usize }>() else { return Ok(None); }; - let token = u32::from_ne_bytes(token.try_into().unwrap()); cursor.offset += TOKEN_SIZE; - let token = match token { + let token = match u32::from_ne_bytes(token) { BEGIN_NODE => RawToken::BeginNode, END_NODE => RawToken::EndNode, PROP => RawToken::Prop, diff --git a/src/lib.rs b/src/lib.rs index c067a23..90dfdeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,6 @@ #[cfg(feature = "alloc")] extern crate alloc as std_alloc; -#[cfg(feature = "std")] -extern crate std; #[cfg(feature = "alloc")] pub mod alloc; @@ -70,8 +68,7 @@ impl Display for Error { } } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} pub type Result = core::result::Result; @@ -115,8 +112,7 @@ impl Display for BlobError { } } -#[cfg(feature = "std")] -impl std::error::Error for BlobError {} +impl core::error::Error for BlobError {} impl From for Error { #[inline] @@ -212,7 +208,7 @@ impl FallibleIterator for MemReserveEntries<'_> { fn next(&mut self) -> Result, Self::Error> { let (raw, _) = blob::RawReserveEntry::read_from_prefix(self.blob.as_bytes()) .map_err(|_| BlobError::UnexpectedEnd)?; - self.blob = &self.blob[blob::RawReserveEntry::FIELD_COUNT..]; + self.blob = &self.blob[blob::RawReserveEntry::NUM_FIELDS..]; let entry = (raw.address != 0 || raw.size != 0).then(|| MemReserveEntry { address: u64::from_be(raw.address), @@ -464,10 +460,11 @@ pub mod util { return None; } let mut ret: u128 = 0; - for &word in value.get(..cells as usize)? { + let (content, rest) = value.split_at_checked(cells as usize)?; + for &word in content { ret = ret << 0x20 | u32::from_be(word) as u128; } - *value = &value[cells as usize..]; + *value = rest; Some(ret) } @@ -495,13 +492,6 @@ pub mod util { AsciiStr::from_ascii(blob).ok().map(AsciiStr::as_str) } - /// Same as `<[_]>::get` with a range except that it takes a length, not an end - /// offset. - #[inline] - pub(crate) fn slice_get_with_len(slice: &[T], offset: usize, len: usize) -> Option<&[T]> { - slice.get(offset..offset + len) - } - /// Same as `<[_]>::get_unchecked` with a range except that it takes a length, /// not an end offset. /// diff --git a/src/prop_value.rs b/src/prop_value.rs index 5ae6cc9..c7be6a2 100644 --- a/src/prop_value.rs +++ b/src/prop_value.rs @@ -561,7 +561,10 @@ impl<'dtb> DeserializeProperty<'dtb> for SmallU64 { } } -/// Zero-sized type that throws if the property value isn't `"memory"`. +/// Zero-sized type that throws [an error] if the property value isn't +/// `"memory"`. +/// +/// [an error]: Error::InvalidDeviceType #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct DeviceTypeMemory; @@ -575,7 +578,9 @@ impl<'dtb> DeserializeProperty<'dtb> for DeviceTypeMemory { } } -/// Zero-sized type that throws if the property value isn't `"cpu"`. +/// Zero-sized type that throws [an error] if the property value isn't `"cpu"`. +/// +/// [an error]: Error::InvalidDeviceType #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct DeviceTypeCpu;