diff --git a/uefi-raw/src/status.rs b/uefi-raw/src/status.rs index 9bc74a784..7fa13df8f 100644 --- a/uefi-raw/src/status.rs +++ b/uefi-raw/src/status.rs @@ -98,7 +98,7 @@ pub enum Status: usize => { impl Status { /// Bit indicating that an UEFI status code is an error. - pub const ERROR_BIT: usize = 1 << (core::mem::size_of::() * 8 - 1); + pub const ERROR_BIT: usize = 1 << (usize::BITS - 1); /// Returns true if status code indicates success. #[inline] diff --git a/uefi-raw/src/table/system.rs b/uefi-raw/src/table/system.rs index 08cd0f5fa..3d68451fc 100644 --- a/uefi-raw/src/table/system.rs +++ b/uefi-raw/src/table/system.rs @@ -4,7 +4,7 @@ use crate::table::configuration::ConfigurationTable; use crate::table::runtime::RuntimeServices; use crate::table::Header; use crate::{Char16, Handle}; -use core::{mem, ptr}; +use core::ptr; #[derive(Clone, Debug, Eq, PartialEq)] #[repr(C)] @@ -44,7 +44,7 @@ impl Default for SystemTable { Self { header: Header { signature: Self::SIGNATURE, - size: u32::try_from(mem::size_of::()).unwrap(), + size: u32::try_from(size_of::()).unwrap(), ..Header::default() }, diff --git a/uefi-test-runner/examples/sierpinski.rs b/uefi-test-runner/examples/sierpinski.rs index 28906e3c9..530a19bb2 100644 --- a/uefi-test-runner/examples/sierpinski.rs +++ b/uefi-test-runner/examples/sierpinski.rs @@ -6,7 +6,6 @@ extern crate alloc; use alloc::vec; use alloc::vec::Vec; -use core::mem; use uefi::prelude::*; use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput}; use uefi::proto::rng::Rng; @@ -77,7 +76,7 @@ impl Buffer { /// Get a random `usize` value. fn get_random_usize(rng: &mut Rng) -> usize { - let mut buf = [0; mem::size_of::()]; + let mut buf = [0; size_of::()]; rng.get_rng(None, &mut buf).expect("get_rng failed"); usize::from_le_bytes(buf) } diff --git a/uefi-test-runner/src/boot/misc.rs b/uefi-test-runner/src/boot/misc.rs index f006f18ff..196ea9dbe 100644 --- a/uefi-test-runner/src/boot/misc.rs +++ b/uefi-test-runner/src/boot/misc.rs @@ -1,5 +1,4 @@ use core::ffi::c_void; -use core::mem; use core::ptr::{self, NonNull}; use uefi::boot::{ @@ -121,13 +120,11 @@ fn test_register_protocol_notify() { fn test_install_protocol_interface() { info!("Installing TestProtocol"); - let alloc: *mut TestProtocol = boot::allocate_pool( - MemoryType::BOOT_SERVICES_DATA, - mem::size_of::(), - ) - .unwrap() - .cast() - .as_ptr(); + let alloc: *mut TestProtocol = + boot::allocate_pool(MemoryType::BOOT_SERVICES_DATA, size_of::()) + .unwrap() + .cast() + .as_ptr(); unsafe { alloc.write(TestProtocol { data: 123 }) }; let _ = unsafe { diff --git a/uefi/src/boot.rs b/uefi/src/boot.rs index 13526d245..48acb6aaf 100644 --- a/uefi/src/boot.rs +++ b/uefi/src/boot.rs @@ -277,7 +277,7 @@ pub(crate) fn get_memory_map(buf: &mut [u8]) -> Result { let mut desc_version = 0; assert_eq!( - (map_buffer as usize) % mem::align_of::(), + (map_buffer as usize) % align_of::(), 0, "Memory map buffers must be aligned like a MemoryDescriptor" ); @@ -793,11 +793,11 @@ pub fn locate_handle<'buf>( SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()), }; - let mut buffer_size = buffer.len() * mem::size_of::(); + let mut buffer_size = buffer.len() * size_of::(); let status = unsafe { (bt.locate_handle)(ty, guid, key, &mut buffer_size, buffer.as_mut_ptr().cast()) }; - let num_handles = buffer_size / mem::size_of::(); + let num_handles = buffer_size / size_of::(); match status { Status::SUCCESS => { diff --git a/uefi/src/data_types/mod.rs b/uefi/src/data_types/mod.rs index a5720d2a4..b178d3d6a 100644 --- a/uefi/src/data_types/mod.rs +++ b/uefi/src/data_types/mod.rs @@ -99,8 +99,8 @@ impl Event { /// Trait for querying the alignment of a struct. /// /// For a statically-sized type the alignment can be retrieved with -/// [`core::mem::align_of`]. For a dynamically-sized type (DST), -/// [`core::mem::align_of_val`] provides the alignment given a reference. But in +/// [`align_of`]. For a dynamically-sized type (DST), +/// [`align_of_val`] provides the alignment given a reference. But in /// some cases it's helpful to know the alignment of a DST prior to having a /// value, meaning there's no reference to pass to `align_of_val`. For example, /// when using an API that creates a value using a `[u8]` buffer, the alignment diff --git a/uefi/src/mem/memory_map/impl_.rs b/uefi/src/mem/memory_map/impl_.rs index e8397071f..5d236da5b 100644 --- a/uefi/src/mem/memory_map/impl_.rs +++ b/uefi/src/mem/memory_map/impl_.rs @@ -5,8 +5,8 @@ use super::*; use crate::boot; use core::fmt::{Debug, Display, Formatter}; use core::ops::{Index, IndexMut}; +use core::ptr; use core::ptr::NonNull; -use core::{mem, ptr}; use uefi_raw::PhysicalAddress; /// Errors that may happen when constructing a [`MemoryMapRef`] or @@ -284,7 +284,7 @@ impl MemoryMapBackingMemory { // Should be fine as UEFI always has allocations with a guaranteed // alignment of 8 bytes. - assert_eq!(ptr.align_offset(mem::align_of::()), 0); + assert_eq!(ptr.align_offset(align_of::()), 0); // If this panics, the UEFI implementation is broken. assert_eq!(memory_map_meta.map_size % memory_map_meta.desc_size, 0); @@ -293,7 +293,7 @@ impl MemoryMapBackingMemory { } unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self { - assert_eq!(ptr.align_offset(mem::align_of::()), 0); + assert_eq!(ptr.align_offset(align_of::()), 0); let ptr = NonNull::new(ptr).expect("UEFI should never return a null ptr. An error should have been reflected via an Err earlier."); let slice = NonNull::slice_from_raw_parts(ptr, len); @@ -367,7 +367,7 @@ impl MemoryMapOwned { /// (stored inside the provided buffer) and the corresponding /// [`MemoryMapMeta`]. pub(crate) fn from_initialized_mem(buf: MemoryMapBackingMemory, meta: MemoryMapMeta) -> Self { - assert!(meta.desc_size >= mem::size_of::()); + assert!(meta.desc_size >= size_of::()); let len = meta.entry_count(); Self { buf, meta, len } } @@ -431,7 +431,7 @@ impl IndexMut for MemoryMapOwned { mod tests { use super::*; use alloc::vec::Vec; - use core::mem::size_of; + use size_of; const BASE_MMAP_UNSORTED: [MemoryDescriptor; 3] = [ MemoryDescriptor { diff --git a/uefi/src/mem/memory_map/mod.rs b/uefi/src/mem/memory_map/mod.rs index 46cd82903..9fead2f6f 100644 --- a/uefi/src/mem/memory_map/mod.rs +++ b/uefi/src/mem/memory_map/mod.rs @@ -41,11 +41,10 @@ pub use iter::*; pub use uefi_raw::table::boot::{MemoryAttribute, MemoryDescriptor, MemoryType}; use crate::data_types::Align; -use core::mem; impl Align for MemoryDescriptor { fn alignment() -> usize { - mem::align_of::() + align_of::() } } @@ -90,7 +89,7 @@ impl MemoryMapMeta { // extended by a future UEFI revision by a significant amount, we // update the struct, but an old UEFI implementation reports a small // size. - assert!(self.desc_size >= mem::size_of::()); + assert!(self.desc_size >= size_of::()); assert!(self.map_size > 0); // Ensure the mmap size is (somehow) sane. @@ -238,8 +237,8 @@ mod tests_mmap_artificial { mod tests_mmap_real { use super::*; use alloc::vec::Vec; - use core::mem::size_of; use core::slice; + use size_of; const MMAP_META: MemoryMapMeta = MemoryMapMeta { map_size: MMAP_RAW.len() * size_of::(), diff --git a/uefi/src/proto/console/gop.rs b/uefi/src/proto/console/gop.rs index 46c004b92..0e20d6c07 100644 --- a/uefi/src/proto/console/gop.rs +++ b/uefi/src/proto/console/gop.rs @@ -55,7 +55,6 @@ use crate::util::usize_from_u32; use crate::{boot, Result, StatusExt}; use core::fmt::{Debug, Formatter}; use core::marker::PhantomData; -use core::mem; use core::ptr::{self, NonNull}; use uefi_raw::protocol::console::{ GraphicsOutputBltOperation, GraphicsOutputModeInformation, GraphicsOutputProtocol, @@ -181,7 +180,7 @@ impl GraphicsOutput { dest_y, width, height, - px_stride * core::mem::size_of::(), + px_stride * size_of::(), ) .to_result(), } @@ -221,7 +220,7 @@ impl GraphicsOutput { dest_y, width, height, - px_stride * core::mem::size_of::(), + px_stride * size_of::(), ) .to_result(), } @@ -620,7 +619,7 @@ impl FrameBuffer<'_> { #[inline] pub unsafe fn write_value(&mut self, index: usize, value: T) { debug_assert!( - index.saturating_add(mem::size_of::()) <= self.size, + index.saturating_add(size_of::()) <= self.size, "Frame buffer accessed out of bounds" ); let ptr = self.base.add(index).cast::(); @@ -643,7 +642,7 @@ impl FrameBuffer<'_> { #[must_use] pub unsafe fn read_value(&self, index: usize) -> T { debug_assert!( - index.saturating_add(mem::size_of::()) <= self.size, + index.saturating_add(size_of::()) <= self.size, "Frame buffer accessed out of bounds" ); (self.base.add(index) as *const T).read_volatile() diff --git a/uefi/src/proto/device_path/build.rs b/uefi/src/proto/device_path/build.rs index 3a09b2451..91c85e72e 100644 --- a/uefi/src/proto/device_path/build.rs +++ b/uefi/src/proto/device_path/build.rs @@ -245,10 +245,10 @@ mod tests { use crate::proto::device_path::messaging::{ Ipv4AddressOrigin, IscsiLoginOptions, IscsiProtocol, RestServiceAccessMode, RestServiceType, }; - use core::{mem, slice}; + use core::slice; fn path_to_bytes(path: &DevicePath) -> &[u8] { - unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::(), mem::size_of_val(path)) } + unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::(), size_of_val(path)) } } /// Test building an ACPI ADR node. diff --git a/uefi/src/proto/device_path/mod.rs b/uefi/src/proto/device_path/mod.rs index 0d59beeed..6e80a2dae 100644 --- a/uefi/src/proto/device_path/mod.rs +++ b/uefi/src/proto/device_path/mod.rs @@ -85,7 +85,6 @@ pub use uefi_raw::protocol::device_path::{DeviceSubType, DeviceType}; use crate::proto::{unsafe_protocol, ProtocolPointer}; use core::ffi::c_void; use core::fmt::{self, Debug, Display, Formatter}; -use core::mem; use core::ops::Deref; use ptr_meta::Pointee; @@ -96,6 +95,7 @@ use { crate::{CString16, Identify}, alloc::borrow::ToOwned, alloc::boxed::Box, + core::mem, }; opaque_type! { @@ -122,7 +122,7 @@ impl<'a> TryFrom<&'a [u8]> for &'a DevicePathHeader { type Error = ByteConversionError; fn try_from(bytes: &[u8]) -> Result { - if mem::size_of::() <= bytes.len() { + if size_of::() <= bytes.len() { unsafe { Ok(&*bytes.as_ptr().cast::()) } } else { Err(ByteConversionError::InvalidLength) @@ -173,7 +173,7 @@ impl DevicePathNode { pub unsafe fn from_ffi_ptr<'a>(ptr: *const FfiDevicePath) -> &'a Self { let header = *ptr.cast::(); - let data_len = usize::from(header.length) - mem::size_of::(); + let data_len = usize::from(header.length) - size_of::(); &*ptr_meta::from_raw_parts(ptr.cast(), data_len) } @@ -748,7 +748,7 @@ mod tests { path.push(device_type); path.push(sub_type); path.extend( - u16::try_from(mem::size_of::() + node_data.len()) + u16::try_from(size_of::() + node_data.len()) .unwrap() .to_le_bytes(), ); @@ -787,7 +787,7 @@ mod tests { assert_eq!(node.sub_type().0, sub_type); assert_eq!( node.length(), - u16::try_from(mem::size_of::() + node_data.len()).unwrap() + u16::try_from(size_of::() + node_data.len()).unwrap() ); assert_eq!(&node.data, node_data); } @@ -798,7 +798,7 @@ mod tests { let dp = unsafe { DevicePath::from_ffi_ptr(raw_data.as_ptr().cast()) }; // Check that the size is the sum of the nodes' lengths. - assert_eq!(mem::size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4); + assert_eq!(size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4); // Check the list's node iter. let nodes: Vec<_> = dp.node_iter().collect(); @@ -824,7 +824,7 @@ mod tests { // Check the list's instance iter. let mut iter = dp.instance_iter(); let mut instance = iter.next().unwrap(); - assert_eq!(mem::size_of_val(instance), 6 + 8 + 4); + assert_eq!(size_of_val(instance), 6 + 8 + 4); // Check the first instance's node iter. let nodes: Vec<_> = instance.node_iter().collect(); @@ -835,7 +835,7 @@ mod tests { // Check second instance. instance = iter.next().unwrap(); - assert_eq!(mem::size_of_val(instance), 6 + 8 + 4); + assert_eq!(size_of_val(instance), 6 + 8 + 4); let nodes: Vec<_> = instance.node_iter().collect(); check_node(nodes[0], 0xa2, 0xb2, &[30, 31]); @@ -876,7 +876,7 @@ mod tests { // Raw data is long enough to hold a [`DevicePathNode`]. raw_data.push(node[1]); raw_data.extend( - u16::try_from(mem::size_of::() + node_data.len()) + u16::try_from(size_of::() + node_data.len()) .unwrap() .to_le_bytes(), ); @@ -884,7 +884,7 @@ mod tests { let dp = <&DevicePathNode>::try_from(raw_data.as_slice()).unwrap(); // Relevant assertions to verify the conversion is fine. - assert_eq!(mem::size_of_val(dp), 6); + assert_eq!(size_of_val(dp), 6); check_node(dp, 0xa0, 0xb0, &[10, 11]); // [`DevicePathNode`] data length exceeds the raw_data slice. @@ -898,7 +898,7 @@ mod tests { let dp = <&DevicePath>::try_from(raw_data.as_slice()).unwrap(); // Check that the size is the sum of the nodes' lengths. - assert_eq!(mem::size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4); + assert_eq!(size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4); // Check the list's node iter. let nodes: Vec<_> = dp.node_iter().collect(); diff --git a/uefi/src/proto/loaded_image.rs b/uefi/src/proto/loaded_image.rs index 057cfa4de..cc2ef5cb4 100644 --- a/uefi/src/proto/loaded_image.rs +++ b/uefi/src/proto/loaded_image.rs @@ -67,15 +67,15 @@ impl LoadedImage { if self.0.load_options.is_null() { Err(LoadOptionsError::NotSet) - } else if (load_options_size % mem::size_of::() != 0) - || (((self.0.load_options as usize) % mem::align_of::()) != 0) + } else if (load_options_size % size_of::() != 0) + || (((self.0.load_options as usize) % align_of::()) != 0) { Err(LoadOptionsError::NotAligned) } else { let s = unsafe { slice::from_raw_parts( self.0.load_options.cast::(), - load_options_size / mem::size_of::(), + load_options_size / size_of::(), ) }; CStr16::from_u16_with_nul(s).map_err(LoadOptionsError::InvalidString) diff --git a/uefi/src/proto/media/file/info.rs b/uefi/src/proto/media/file/info.rs index 1ed1df8bf..87108b36d 100644 --- a/uefi/src/proto/media/file/info.rs +++ b/uefi/src/proto/media/file/info.rs @@ -4,7 +4,7 @@ use crate::runtime::Time; use crate::{CStr16, Char16, Guid, Identify}; use core::ffi::c_void; use core::fmt::{self, Display, Formatter}; -use core::{mem, ptr}; +use core::ptr; use ptr_meta::Pointee; /// Common trait for data structures that can be used with @@ -69,7 +69,7 @@ trait InfoInternal: Align + ptr_meta::Pointee { { // Calculate the final size of the struct. let name_length_ucs2 = name.as_slice_with_nul().len(); - let name_size = mem::size_of_val(name.as_slice_with_nul()); + let name_size = size_of_val(name.as_slice_with_nul()); let info_size = Self::name_offset() + name_size; let info_size = Self::round_up_to_alignment(info_size); @@ -432,7 +432,7 @@ mod tests { fn validate_layout(info: &T, name: &[Char16]) { // Check the hardcoded struct alignment. - assert_eq!(mem::align_of_val(info), T::alignment()); + assert_eq!(align_of_val(info), T::alignment()); // Check the hardcoded name slice offset. assert_eq!( unsafe { (name.as_ptr() as *const u8).offset_from(info as *const _ as *const u8) }, @@ -481,7 +481,7 @@ mod tests { // = 100 // Round size up to match FileInfo alignment of 8: 104 assert_eq!(info.size, 104); - assert_eq!(info.size, mem::size_of_val(info) as u64); + assert_eq!(info.size, size_of_val(info) as u64); assert_eq!(info.file_size(), file_size); assert_eq!(info.physical_size(), physical_size); @@ -518,7 +518,7 @@ mod tests { // = 58 // Round size up to match FileSystemInfo alignment of 8: 64 assert_eq!(info.size, 64); - assert_eq!(info.size, mem::size_of_val(info) as u64); + assert_eq!(info.size, size_of_val(info) as u64); assert_eq!(info.read_only, read_only); assert_eq!(info.volume_size, volume_size); diff --git a/uefi/src/proto/media/file/mod.rs b/uefi/src/proto/media/file/mod.rs index f9e7ad033..44f8273f2 100644 --- a/uefi/src/proto/media/file/mod.rs +++ b/uefi/src/proto/media/file/mod.rs @@ -170,7 +170,7 @@ pub trait File: Sized { /// * [`uefi::Status::BAD_BUFFER_SIZE`] fn set_info(&mut self, info: &Info) -> Result { let info_ptr = ptr::from_ref(info).cast::(); - let info_size = mem::size_of_val(info); + let info_size = size_of_val(info); unsafe { (self.imp().set_info)(self.imp(), &Info::GUID, info_size, info_ptr).to_result() } } @@ -404,7 +404,7 @@ mod tests { &CString16::try_from("test_file").unwrap(), ) .unwrap(); - let required_size = mem::size_of_val(info); + let required_size = size_of_val(info); if *buffer_size < required_size { *buffer_size = required_size; Status::BUFFER_TOO_SMALL diff --git a/uefi/src/proto/network/pxe.rs b/uefi/src/proto/network/pxe.rs index 84be71b18..5946bb4d8 100644 --- a/uefi/src/proto/network/pxe.rs +++ b/uefi/src/proto/network/pxe.rs @@ -678,10 +678,10 @@ impl DiscoverInfo { let server_count = srv_list.len(); assert!(server_count <= u16::MAX as usize, "too many servers"); - let required_size = core::mem::size_of::() * 4 - + core::mem::size_of::() - + core::mem::size_of::() - + core::mem::size_of_val(srv_list); + let required_size = size_of::() * 4 + + size_of::() + + size_of::() + + size_of_val(srv_list); if buffer.len() < required_size { return Err(Status::BUFFER_TOO_SMALL.into()); diff --git a/uefi/src/proto/network/snp.rs b/uefi/src/proto/network/snp.rs index 05a7ffd7f..a12f2e498 100644 --- a/uefi/src/proto/network/snp.rs +++ b/uefi/src/proto/network/snp.rs @@ -158,7 +158,7 @@ impl SimpleNetwork { /// Collect statistics on a network interface. pub fn collect_statistics(&self) -> Result { let mut stats_table: NetworkStats = Default::default(); - let mut stats_size = core::mem::size_of::(); + let mut stats_size = size_of::(); let status = (self.statistics)(self, false, Some(&mut stats_size), Some(&mut stats_table)); status.to_result_with_val(|| stats_table) } diff --git a/uefi/src/proto/rng.rs b/uefi/src/proto/rng.rs index a973f9844..3a50854e7 100644 --- a/uefi/src/proto/rng.rs +++ b/uefi/src/proto/rng.rs @@ -2,7 +2,7 @@ use crate::proto::unsafe_protocol; use crate::{Result, Status, StatusExt}; -use core::{mem, ptr}; +use core::ptr; pub use uefi_raw::protocol::rng::RngAlgorithmType; @@ -18,7 +18,7 @@ impl Rng { &mut self, algorithm_list: &'buf mut [RngAlgorithmType], ) -> Result<&'buf [RngAlgorithmType], Option> { - let mut algorithm_list_size = mem::size_of_val(algorithm_list); + let mut algorithm_list_size = size_of_val(algorithm_list); unsafe { (self.0.get_info)( @@ -28,7 +28,7 @@ impl Rng { ) .to_result_with( || { - let len = algorithm_list_size / mem::size_of::(); + let len = algorithm_list_size / size_of::(); &algorithm_list[..len] }, |status| { diff --git a/uefi/src/proto/tcg/v1.rs b/uefi/src/proto/tcg/v1.rs index 4b9572a81..341106560 100644 --- a/uefi/src/proto/tcg/v1.rs +++ b/uefi/src/proto/tcg/v1.rs @@ -15,7 +15,7 @@ use crate::util::{ptr_write_unaligned_and_add, usize_from_u32}; use crate::{Error, Result, Status, StatusExt}; use core::fmt::{self, Debug, Formatter}; use core::marker::PhantomData; -use core::{mem, ptr}; +use core::ptr; use ptr_meta::Pointee; use uefi_raw::protocol::tcg::v1::{TcgBootServiceCapability, TcgProtocol}; @@ -117,10 +117,10 @@ impl PcrEvent { let event_data_size = u32::try_from(event_data.len()) .map_err(|_| Error::new(Status::INVALID_PARAMETER, None))?; - let required_size = mem::size_of::() - + mem::size_of::() - + mem::size_of::() - + mem::size_of::() + let required_size = size_of::() + + size_of::() + + size_of::() + + size_of::() + event_data.len(); if buffer.len() < required_size { @@ -322,7 +322,7 @@ impl<'a> Iterator for EventLogIter<'a> { if self.location == self.log.last_entry { self.location = ptr::null(); } else { - self.location = unsafe { self.location.add(mem::size_of_val(event)) }; + self.location = unsafe { self.location.add(size_of_val(event)) }; } Some(event) @@ -512,8 +512,7 @@ mod tests { assert_eq!(event.event_data(), data); let event_ptr: *const PcrEvent = event; - let bytes = - unsafe { slice::from_raw_parts(event_ptr.cast::(), mem::size_of_val(event)) }; + let bytes = unsafe { slice::from_raw_parts(event_ptr.cast::(), size_of_val(event)) }; #[rustfmt::skip] assert_eq!(bytes, [ // PCR index diff --git a/uefi/src/proto/tcg/v2.rs b/uefi/src/proto/tcg/v2.rs index 4816ed06b..e427ed7ec 100644 --- a/uefi/src/proto/tcg/v2.rs +++ b/uefi/src/proto/tcg/v2.rs @@ -17,7 +17,7 @@ use crate::util::{ptr_write_unaligned_and_add, usize_from_u32}; use crate::{Error, Result, Status, StatusExt}; use core::fmt::{self, Debug, Formatter}; use core::marker::PhantomData; -use core::{mem, ptr, slice}; +use core::{ptr, slice}; use ptr_meta::Pointee; use uefi_raw::protocol::tcg::v2::{Tcg2EventHeader as EventHeader, Tcg2Protocol}; @@ -80,7 +80,7 @@ pub struct BootServiceCapability { impl Default for BootServiceCapability { fn default() -> Self { // OK to unwrap, the size is less than u8. - let struct_size = u8::try_from(mem::size_of::()).unwrap(); + let struct_size = u8::try_from(size_of::()).unwrap(); Self { size: struct_size, @@ -140,8 +140,7 @@ impl PcrEventInputs { event_type: EventType, event_data: &[u8], ) -> Result<&'buf mut Self, Option> { - let required_size = - mem::size_of::() + mem::size_of::() + event_data.len(); + let required_size = size_of::() + size_of::() + event_data.len(); if buffer.len() < required_size { return Err(Error::new(Status::BUFFER_TOO_SMALL, Some(required_size))); @@ -156,7 +155,7 @@ impl PcrEventInputs { ptr_write_unaligned_and_add( &mut ptr, EventHeader { - header_size: u32::try_from(mem::size_of::()).unwrap(), + header_size: u32::try_from(size_of::()).unwrap(), header_version: 1, pcr_index: pcr_index.0, event_type, @@ -289,7 +288,7 @@ impl<'a> EventLogHeader<'a> { let uintn_size = *event.get(23)?; let number_of_algorithms = usize_from_u32(u32_le_from_bytes_at_offset(event, 24)?); let vendor_info_size_byte_offset = - 28 + (number_of_algorithms * mem::size_of::()); + 28 + (number_of_algorithms * size_of::()); let vendor_info_size = usize::from(*event.get(vendor_info_size_byte_offset)?); // Safety: we know the slice is big enough because we just @@ -462,7 +461,7 @@ impl<'a> PcrEvent<'a> { let mut elem_ptr = digests_ptr; for _ in 0..digests_count { let algorithm_id = AlgorithmId(elem_ptr.cast::().read_unaligned()); - let alg_and_digest_size = mem::size_of::() + let alg_and_digest_size = size_of::() + usize::from(header.algorithm_digest_sizes.get_size(algorithm_id)?); digests_byte_size += alg_and_digest_size; elem_ptr = elem_ptr.add(alg_and_digest_size); @@ -768,7 +767,7 @@ mod tests { // Cast to a byte slice to check the data is exactly as expected. let event_ptr: *const PcrEventInputs = event; let event_ptr: *const u8 = event_ptr.cast(); - let event_bytes = unsafe { slice::from_raw_parts(event_ptr, mem::size_of_val(event)) }; + let event_bytes = unsafe { slice::from_raw_parts(event_ptr, size_of_val(event)) }; #[rustfmt::skip] assert_eq!(event_bytes, [ diff --git a/uefi/src/runtime.rs b/uefi/src/runtime.rs index b8dfd9878..f99d1d0ca 100644 --- a/uefi/src/runtime.rs +++ b/uefi/src/runtime.rs @@ -9,7 +9,6 @@ use crate::data_types::PhysicalAddress; use crate::table::{self, Revision}; use crate::{CStr16, Error, Result, Status, StatusExt}; use core::fmt::{self, Debug, Display, Formatter}; -use core::mem; use core::ptr::{self, NonNull}; use uefi_raw::table::boot::MemoryDescriptor; @@ -225,7 +224,7 @@ pub fn get_next_variable_key( let rt = runtime_services_raw_panicking(); let rt = unsafe { rt.as_ref() }; - let mut name_size_in_bytes = mem::size_of_val(name); + let mut name_size_in_bytes = size_of_val(name); let status = unsafe { (rt.get_next_variable_name)(&mut name_size_in_bytes, name.as_mut_ptr(), &mut vendor.0) @@ -234,7 +233,7 @@ pub fn get_next_variable_key( Status::SUCCESS => Ok(()), Status::BUFFER_TOO_SMALL => Err(Error::new( status, - Some(name_size_in_bytes / mem::size_of::()), + Some(name_size_in_bytes / size_of::()), )), _ => Err(Error::new(status, None)), } @@ -539,8 +538,8 @@ pub unsafe fn set_virtual_address_map( // between its elements if the element type is `repr(C)`, which is our case. // // See https://rust-lang.github.io/unsafe-code-guidelines/layout/arrays-and-slices.html - let map_size = core::mem::size_of_val(map); - let entry_size = core::mem::size_of::(); + let map_size = size_of_val(map); + let entry_size = size_of::(); let entry_version = MemoryDescriptor::VERSION; let map_ptr = map.as_mut_ptr(); (rt.set_virtual_address_map)(map_size, entry_size, entry_version, map_ptr).to_result()?; @@ -825,7 +824,7 @@ impl TryFrom<&[u8]> for Time { type Error = TimeByteConversionError; fn try_from(bytes: &[u8]) -> core::result::Result { - if mem::size_of::() <= bytes.len() { + if size_of::() <= bytes.len() { let year = u16::from_le_bytes(bytes[0..2].try_into().unwrap()); let month = bytes[2]; let day = bytes[3]; diff --git a/uefi/src/util.rs b/uefi/src/util.rs index 929ca6df8..1c3202d17 100644 --- a/uefi/src/util.rs +++ b/uefi/src/util.rs @@ -1,11 +1,10 @@ -use core::mem; use core::ptr::{self, NonNull}; /// Copy the bytes of `val` to `ptr`, then advance pointer to just after the /// newly-copied bytes. pub unsafe fn ptr_write_unaligned_and_add(ptr: &mut *mut u8, val: T) { ptr.cast::().write_unaligned(val); - *ptr = ptr.add(mem::size_of::()); + *ptr = ptr.add(size_of::()); } /// Convert from a `u32` to a `usize`. Panic if the input does fit. On typical @@ -18,7 +17,7 @@ pub unsafe fn ptr_write_unaligned_and_add(ptr: &mut *mut u8, val: T) { pub const fn usize_from_u32(val: u32) -> usize { // This is essentially the same as `usize::try_from(val).unwrap()`, but // works in a `const` context on stable. - if mem::size_of::() < mem::size_of::() && val < (usize::MAX as u32) { + if size_of::() < size_of::() && val < (usize::MAX as u32) { panic!("value does not fit in a usize"); } else { val as usize