Skip to content

Commit a5e9ba1

Browse files
Use size_of/size_of_val/align_of/align_of_val from the prelude
As of Rust 1.80, these functions are in the prelude so we don't need to import `mem` or fully specify the path.
1 parent 333886e commit a5e9ba1

File tree

20 files changed

+77
-87
lines changed

20 files changed

+77
-87
lines changed

uefi-raw/src/table/system.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::table::configuration::ConfigurationTable;
44
use crate::table::runtime::RuntimeServices;
55
use crate::table::Header;
66
use crate::{Char16, Handle};
7-
use core::{mem, ptr};
7+
use core::ptr;
88

99
#[derive(Clone, Debug, Eq, PartialEq)]
1010
#[repr(C)]
@@ -44,7 +44,7 @@ impl Default for SystemTable {
4444
Self {
4545
header: Header {
4646
signature: Self::SIGNATURE,
47-
size: u32::try_from(mem::size_of::<Self>()).unwrap(),
47+
size: u32::try_from(size_of::<Self>()).unwrap(),
4848
..Header::default()
4949
},
5050

uefi-test-runner/examples/sierpinski.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ extern crate alloc;
66

77
use alloc::vec;
88
use alloc::vec::Vec;
9-
use core::mem;
109
use uefi::prelude::*;
1110
use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
1211
use uefi::proto::rng::Rng;
@@ -77,7 +76,7 @@ impl Buffer {
7776

7877
/// Get a random `usize` value.
7978
fn get_random_usize(rng: &mut Rng) -> usize {
80-
let mut buf = [0; mem::size_of::<usize>()];
79+
let mut buf = [0; size_of::<usize>()];
8180
rng.get_rng(None, &mut buf).expect("get_rng failed");
8281
usize::from_le_bytes(buf)
8382
}

uefi-test-runner/src/boot/misc.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use core::ffi::c_void;
2-
use core::mem;
32
use core::ptr::{self, NonNull};
43

54
use uefi::boot::{
@@ -121,13 +120,11 @@ fn test_register_protocol_notify() {
121120
fn test_install_protocol_interface() {
122121
info!("Installing TestProtocol");
123122

124-
let alloc: *mut TestProtocol = boot::allocate_pool(
125-
MemoryType::BOOT_SERVICES_DATA,
126-
mem::size_of::<TestProtocol>(),
127-
)
128-
.unwrap()
129-
.cast()
130-
.as_ptr();
123+
let alloc: *mut TestProtocol =
124+
boot::allocate_pool(MemoryType::BOOT_SERVICES_DATA, size_of::<TestProtocol>())
125+
.unwrap()
126+
.cast()
127+
.as_ptr();
131128
unsafe { alloc.write(TestProtocol { data: 123 }) };
132129

133130
let _ = unsafe {

uefi/src/boot.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub(crate) fn get_memory_map(buf: &mut [u8]) -> Result<MemoryMapMeta> {
277277
let mut desc_version = 0;
278278

279279
assert_eq!(
280-
(map_buffer as usize) % mem::align_of::<MemoryDescriptor>(),
280+
(map_buffer as usize) % align_of::<MemoryDescriptor>(),
281281
0,
282282
"Memory map buffers must be aligned like a MemoryDescriptor"
283283
);
@@ -793,11 +793,11 @@ pub fn locate_handle<'buf>(
793793
SearchType::ByProtocol(guid) => (2, ptr::from_ref(guid), ptr::null()),
794794
};
795795

796-
let mut buffer_size = buffer.len() * mem::size_of::<Handle>();
796+
let mut buffer_size = buffer.len() * size_of::<Handle>();
797797
let status =
798798
unsafe { (bt.locate_handle)(ty, guid, key, &mut buffer_size, buffer.as_mut_ptr().cast()) };
799799

800-
let num_handles = buffer_size / mem::size_of::<Handle>();
800+
let num_handles = buffer_size / size_of::<Handle>();
801801

802802
match status {
803803
Status::SUCCESS => {

uefi/src/data_types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ impl Event {
9999
/// Trait for querying the alignment of a struct.
100100
///
101101
/// For a statically-sized type the alignment can be retrieved with
102-
/// [`core::mem::align_of`]. For a dynamically-sized type (DST),
103-
/// [`core::mem::align_of_val`] provides the alignment given a reference. But in
102+
/// [`align_of`]. For a dynamically-sized type (DST),
103+
/// [`align_of_val`] provides the alignment given a reference. But in
104104
/// some cases it's helpful to know the alignment of a DST prior to having a
105105
/// value, meaning there's no reference to pass to `align_of_val`. For example,
106106
/// when using an API that creates a value using a `[u8]` buffer, the alignment

uefi/src/mem/memory_map/impl_.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use super::*;
55
use crate::boot;
66
use core::fmt::{Debug, Display, Formatter};
77
use core::ops::{Index, IndexMut};
8+
use core::ptr;
89
use core::ptr::NonNull;
9-
use core::{mem, ptr};
1010
use uefi_raw::PhysicalAddress;
1111

1212
/// Errors that may happen when constructing a [`MemoryMapRef`] or
@@ -284,7 +284,7 @@ impl MemoryMapBackingMemory {
284284

285285
// Should be fine as UEFI always has allocations with a guaranteed
286286
// alignment of 8 bytes.
287-
assert_eq!(ptr.align_offset(mem::align_of::<MemoryDescriptor>()), 0);
287+
assert_eq!(ptr.align_offset(align_of::<MemoryDescriptor>()), 0);
288288

289289
// If this panics, the UEFI implementation is broken.
290290
assert_eq!(memory_map_meta.map_size % memory_map_meta.desc_size, 0);
@@ -293,7 +293,7 @@ impl MemoryMapBackingMemory {
293293
}
294294

295295
unsafe fn from_raw(ptr: *mut u8, len: usize) -> Self {
296-
assert_eq!(ptr.align_offset(mem::align_of::<MemoryDescriptor>()), 0);
296+
assert_eq!(ptr.align_offset(align_of::<MemoryDescriptor>()), 0);
297297

298298
let ptr = NonNull::new(ptr).expect("UEFI should never return a null ptr. An error should have been reflected via an Err earlier.");
299299
let slice = NonNull::slice_from_raw_parts(ptr, len);
@@ -367,7 +367,7 @@ impl MemoryMapOwned {
367367
/// (stored inside the provided buffer) and the corresponding
368368
/// [`MemoryMapMeta`].
369369
pub(crate) fn from_initialized_mem(buf: MemoryMapBackingMemory, meta: MemoryMapMeta) -> Self {
370-
assert!(meta.desc_size >= mem::size_of::<MemoryDescriptor>());
370+
assert!(meta.desc_size >= size_of::<MemoryDescriptor>());
371371
let len = meta.entry_count();
372372
Self { buf, meta, len }
373373
}
@@ -431,7 +431,7 @@ impl IndexMut<usize> for MemoryMapOwned {
431431
mod tests {
432432
use super::*;
433433
use alloc::vec::Vec;
434-
use core::mem::size_of;
434+
use size_of;
435435

436436
const BASE_MMAP_UNSORTED: [MemoryDescriptor; 3] = [
437437
MemoryDescriptor {

uefi/src/mem/memory_map/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ pub use iter::*;
4141
pub use uefi_raw::table::boot::{MemoryAttribute, MemoryDescriptor, MemoryType};
4242

4343
use crate::data_types::Align;
44-
use core::mem;
4544

4645
impl Align for MemoryDescriptor {
4746
fn alignment() -> usize {
48-
mem::align_of::<Self>()
47+
align_of::<Self>()
4948
}
5049
}
5150

@@ -90,7 +89,7 @@ impl MemoryMapMeta {
9089
// extended by a future UEFI revision by a significant amount, we
9190
// update the struct, but an old UEFI implementation reports a small
9291
// size.
93-
assert!(self.desc_size >= mem::size_of::<MemoryDescriptor>());
92+
assert!(self.desc_size >= size_of::<MemoryDescriptor>());
9493
assert!(self.map_size > 0);
9594

9695
// Ensure the mmap size is (somehow) sane.
@@ -238,8 +237,8 @@ mod tests_mmap_artificial {
238237
mod tests_mmap_real {
239238
use super::*;
240239
use alloc::vec::Vec;
241-
use core::mem::size_of;
242240
use core::slice;
241+
use size_of;
243242

244243
const MMAP_META: MemoryMapMeta = MemoryMapMeta {
245244
map_size: MMAP_RAW.len() * size_of::<u64>(),

uefi/src/proto/console/gop.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use crate::util::usize_from_u32;
5555
use crate::{boot, Result, StatusExt};
5656
use core::fmt::{Debug, Formatter};
5757
use core::marker::PhantomData;
58-
use core::mem;
5958
use core::ptr::{self, NonNull};
6059
use uefi_raw::protocol::console::{
6160
GraphicsOutputBltOperation, GraphicsOutputModeInformation, GraphicsOutputProtocol,
@@ -181,7 +180,7 @@ impl GraphicsOutput {
181180
dest_y,
182181
width,
183182
height,
184-
px_stride * core::mem::size_of::<BltPixel>(),
183+
px_stride * size_of::<BltPixel>(),
185184
)
186185
.to_result(),
187186
}
@@ -221,7 +220,7 @@ impl GraphicsOutput {
221220
dest_y,
222221
width,
223222
height,
224-
px_stride * core::mem::size_of::<BltPixel>(),
223+
px_stride * size_of::<BltPixel>(),
225224
)
226225
.to_result(),
227226
}
@@ -620,7 +619,7 @@ impl FrameBuffer<'_> {
620619
#[inline]
621620
pub unsafe fn write_value<T>(&mut self, index: usize, value: T) {
622621
debug_assert!(
623-
index.saturating_add(mem::size_of::<T>()) <= self.size,
622+
index.saturating_add(size_of::<T>()) <= self.size,
624623
"Frame buffer accessed out of bounds"
625624
);
626625
let ptr = self.base.add(index).cast::<T>();
@@ -643,7 +642,7 @@ impl FrameBuffer<'_> {
643642
#[must_use]
644643
pub unsafe fn read_value<T>(&self, index: usize) -> T {
645644
debug_assert!(
646-
index.saturating_add(mem::size_of::<T>()) <= self.size,
645+
index.saturating_add(size_of::<T>()) <= self.size,
647646
"Frame buffer accessed out of bounds"
648647
);
649648
(self.base.add(index) as *const T).read_volatile()

uefi/src/proto/device_path/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ mod tests {
245245
use crate::proto::device_path::messaging::{
246246
Ipv4AddressOrigin, IscsiLoginOptions, IscsiProtocol, RestServiceAccessMode, RestServiceType,
247247
};
248-
use core::{mem, slice};
248+
use core::slice;
249249

250250
fn path_to_bytes(path: &DevicePath) -> &[u8] {
251-
unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::<u8>(), mem::size_of_val(path)) }
251+
unsafe { slice::from_raw_parts(path.as_ffi_ptr().cast::<u8>(), size_of_val(path)) }
252252
}
253253

254254
/// Test building an ACPI ADR node.

uefi/src/proto/device_path/mod.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ pub use uefi_raw::protocol::device_path::{DeviceSubType, DeviceType};
8585
use crate::proto::{unsafe_protocol, ProtocolPointer};
8686
use core::ffi::c_void;
8787
use core::fmt::{self, Debug, Display, Formatter};
88-
use core::mem;
8988
use core::ops::Deref;
9089
use ptr_meta::Pointee;
9190

@@ -96,6 +95,7 @@ use {
9695
crate::{CString16, Identify},
9796
alloc::borrow::ToOwned,
9897
alloc::boxed::Box,
98+
core::mem,
9999
};
100100

101101
opaque_type! {
@@ -122,7 +122,7 @@ impl<'a> TryFrom<&'a [u8]> for &'a DevicePathHeader {
122122
type Error = ByteConversionError;
123123

124124
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
125-
if mem::size_of::<DevicePathHeader>() <= bytes.len() {
125+
if size_of::<DevicePathHeader>() <= bytes.len() {
126126
unsafe { Ok(&*bytes.as_ptr().cast::<DevicePathHeader>()) }
127127
} else {
128128
Err(ByteConversionError::InvalidLength)
@@ -173,7 +173,7 @@ impl DevicePathNode {
173173
pub unsafe fn from_ffi_ptr<'a>(ptr: *const FfiDevicePath) -> &'a Self {
174174
let header = *ptr.cast::<DevicePathHeader>();
175175

176-
let data_len = usize::from(header.length) - mem::size_of::<DevicePathHeader>();
176+
let data_len = usize::from(header.length) - size_of::<DevicePathHeader>();
177177
&*ptr_meta::from_raw_parts(ptr.cast(), data_len)
178178
}
179179

@@ -748,7 +748,7 @@ mod tests {
748748
path.push(device_type);
749749
path.push(sub_type);
750750
path.extend(
751-
u16::try_from(mem::size_of::<DevicePathHeader>() + node_data.len())
751+
u16::try_from(size_of::<DevicePathHeader>() + node_data.len())
752752
.unwrap()
753753
.to_le_bytes(),
754754
);
@@ -787,7 +787,7 @@ mod tests {
787787
assert_eq!(node.sub_type().0, sub_type);
788788
assert_eq!(
789789
node.length(),
790-
u16::try_from(mem::size_of::<DevicePathHeader>() + node_data.len()).unwrap()
790+
u16::try_from(size_of::<DevicePathHeader>() + node_data.len()).unwrap()
791791
);
792792
assert_eq!(&node.data, node_data);
793793
}
@@ -798,7 +798,7 @@ mod tests {
798798
let dp = unsafe { DevicePath::from_ffi_ptr(raw_data.as_ptr().cast()) };
799799

800800
// Check that the size is the sum of the nodes' lengths.
801-
assert_eq!(mem::size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4);
801+
assert_eq!(size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4);
802802

803803
// Check the list's node iter.
804804
let nodes: Vec<_> = dp.node_iter().collect();
@@ -824,7 +824,7 @@ mod tests {
824824
// Check the list's instance iter.
825825
let mut iter = dp.instance_iter();
826826
let mut instance = iter.next().unwrap();
827-
assert_eq!(mem::size_of_val(instance), 6 + 8 + 4);
827+
assert_eq!(size_of_val(instance), 6 + 8 + 4);
828828

829829
// Check the first instance's node iter.
830830
let nodes: Vec<_> = instance.node_iter().collect();
@@ -835,7 +835,7 @@ mod tests {
835835

836836
// Check second instance.
837837
instance = iter.next().unwrap();
838-
assert_eq!(mem::size_of_val(instance), 6 + 8 + 4);
838+
assert_eq!(size_of_val(instance), 6 + 8 + 4);
839839

840840
let nodes: Vec<_> = instance.node_iter().collect();
841841
check_node(nodes[0], 0xa2, 0xb2, &[30, 31]);
@@ -876,15 +876,15 @@ mod tests {
876876
// Raw data is long enough to hold a [`DevicePathNode`].
877877
raw_data.push(node[1]);
878878
raw_data.extend(
879-
u16::try_from(mem::size_of::<DevicePathHeader>() + node_data.len())
879+
u16::try_from(size_of::<DevicePathHeader>() + node_data.len())
880880
.unwrap()
881881
.to_le_bytes(),
882882
);
883883
raw_data.extend(node_data);
884884
let dp = <&DevicePathNode>::try_from(raw_data.as_slice()).unwrap();
885885

886886
// Relevant assertions to verify the conversion is fine.
887-
assert_eq!(mem::size_of_val(dp), 6);
887+
assert_eq!(size_of_val(dp), 6);
888888
check_node(dp, 0xa0, 0xb0, &[10, 11]);
889889

890890
// [`DevicePathNode`] data length exceeds the raw_data slice.
@@ -898,7 +898,7 @@ mod tests {
898898
let dp = <&DevicePath>::try_from(raw_data.as_slice()).unwrap();
899899

900900
// Check that the size is the sum of the nodes' lengths.
901-
assert_eq!(mem::size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4);
901+
assert_eq!(size_of_val(dp), 6 + 8 + 4 + 6 + 8 + 4);
902902

903903
// Check the list's node iter.
904904
let nodes: Vec<_> = dp.node_iter().collect();

0 commit comments

Comments
 (0)