Skip to content

Commit d198935

Browse files
committed
Make MemoryType a newtype enum too, extend the set of automatic derives
1 parent 005d163 commit d198935

File tree

7 files changed

+33
-31
lines changed

7 files changed

+33
-31
lines changed

src/data_types/enums.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
/// Interface a C-style enum as an integer newtype.
1818
///
1919
/// This macro implements Debug for you, the way you would expect it to work on
20-
/// Rust enums (printing the variant name instead of its integer value). If you
21-
/// want anything else to be derived, you need to ask for it.
20+
/// Rust enums (printing the variant name instead of its integer value). It also
21+
/// derives Clone, Copy, Eq and PartialEq, since that always makes sense for
22+
/// C-style enums and is used by the implementation. If you want anything else
23+
/// to be derived, you can ask for it by adding extra derives as shown in the
24+
/// example below.
2225
///
2326
/// One minor annoyance is that since variants will be translated into
2427
/// associated constants in a separate impl block, you need to discriminate
@@ -28,7 +31,7 @@
2831
/// Usage example:
2932
/// ```
3033
/// newtype_enum! {
31-
/// #[derive(Copy, Clone)]
34+
/// #[derive(Cmp, PartialCmp)]
3235
/// pub enum UnixBool: i32 => #[allow(missing_docs)] {
3336
/// FALSE = 0,
3437
/// TRUE = 1,
@@ -49,6 +52,7 @@ macro_rules! newtype_enum {
4952
) => {
5053
$(#[$type_attrs])*
5154
#[repr(transparent)]
55+
#[derive(Clone, Copy, Eq, PartialEq)]
5256
pub struct $type($base_integer);
5357

5458
$(#[$impl_attrs])*

src/error/status.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ newtype_enum! {
1313
/// enum, as injecting an unknown value in a Rust enum is undefined behaviour.
1414
///
1515
/// For lack of a better option, we therefore model them as a newtype of usize.
16-
#[derive(Copy, Clone, Eq, PartialEq)]
1716
#[must_use]
1817
pub enum Status: usize => {
1918
/// The operation completed successfully.

src/proto/console/text/input.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ newtype_enum! {
6464
/// Codes 0x8000 -> 0xFFFF are reserved for future OEM extensibility, therefore
6565
/// this C enum is _not_ safe to model as a Rust enum (where the compiler must
6666
/// know about all variants at compile time).
67-
#[derive(Copy, Clone, Eq, PartialEq)]
6867
pub enum ScanCode: u16 => #[allow(missing_docs)] {
6968
/// Null scan code, indicates that the Unicode character should be used.
7069
NULL = 0x00,

src/proto/debug/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ newtype_enum! {
2828
/// UEFI can be and has been ported to new CPU architectures in the past,
2929
/// therefore modeling this C enum as a Rust enum (where the compiler must know
3030
/// about every variant in existence) would _not_ be safe.
31-
#[derive(Copy, Clone, Eq, PartialEq)]
3231
pub enum ProcessorArch: u32 => {
3332
/// 32-bit x86 PC
3433
X86_32 = 0x014C,

src/table/boot.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -407,50 +407,51 @@ pub enum AllocateType {
407407
Address(usize),
408408
}
409409

410+
newtype_enum! {
410411
/// The type of a memory range.
411-
#[derive(Debug, Copy, Clone)]
412-
#[repr(u32)]
413-
pub enum MemoryType {
412+
///
413+
/// UEFI allows firmwares and operating systems to introduce new memory types
414+
/// in the 0x70000000..0xFFFFFFFF range. Therefore, we don't know the full set
415+
/// of memory types at compile time, and it is _not_ safe to model this C enum
416+
/// as a Rust enum.
417+
pub enum MemoryType: u32 => {
414418
/// This enum variant is not used.
415-
Reserved,
419+
RESERVED = 0,
416420
/// The code portions of a loaded UEFI application.
417-
LoaderCode,
421+
LOADER_CODE = 1,
418422
/// The data portions of a loaded UEFI applications,
419423
/// as well as any memory allocated by it.
420-
LoaderData,
424+
LOADER_DATA = 2,
421425
/// Code of the boot drivers.
422426
///
423427
/// Can be reused after OS is loaded.
424-
BootServicesCode,
428+
BOOT_SERVICES_CODE = 3,
425429
/// Memory used to store boot drivers' data.
426430
///
427431
/// Can be reused after OS is loaded.
428-
BootServicesData,
432+
BOOT_SERVICES_DATA = 4,
429433
/// Runtime drivers' code.
430-
RuntimeServicesCode,
434+
RUNTIME_SERVICES_CODE = 5,
431435
/// Runtime services' code.
432-
RuntimeServicesData,
436+
RUNTIME_SERVICES_DATA = 6,
433437
/// Free usable memory.
434-
Conventional,
438+
CONVENTIONAL = 7,
435439
/// Memory in which errors have been detected.
436-
Unusable,
440+
UNUSABLE = 8,
437441
/// Memory that holds ACPI tables.
438442
/// Can be reclaimed after they are parsed.
439-
AcpiReclaim,
443+
ACPI_RECLAIM = 9,
440444
/// Firmware-reserved addresses.
441-
AcpiNonVolatile,
445+
ACPI_NON_VOLATILE = 10,
442446
/// A region used for memory-mapped I/O.
443-
Mmio,
447+
MMIO = 11,
444448
/// Address space used for memory-mapped port I/O.
445-
MmioPortSpace,
449+
MMIO_PORT_SPACE = 12,
446450
/// Address space which is part of the processor.
447-
PalCode,
451+
PAL_CODE = 13,
448452
/// Memory region which is usable and is also non-volatile.
449-
PersistentMemory,
450-
// SAFETY: UEFI defines a MaxMemoryType, therefore adding new memory types
451-
// would be a breaking change, and exposing them as a Rust enum
452-
// seems to be safe.
453-
}
453+
PERSISTENT_MEMORY = 14,
454+
}}
454455

455456
/// A structure describing a region of memory.
456457
#[derive(Debug, Copy, Clone)]
@@ -473,7 +474,7 @@ pub struct MemoryDescriptor {
473474
impl Default for MemoryDescriptor {
474475
fn default() -> MemoryDescriptor {
475476
MemoryDescriptor {
476-
ty: MemoryType::Reserved,
477+
ty: MemoryType::RESERVED,
477478
padding: 0,
478479
phys_start: 0,
479480
virt_start: 0,

uefi-alloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct Allocator;
4242

4343
unsafe impl GlobalAlloc for Allocator {
4444
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
45-
let mem_ty = MemoryType::LoaderData;
45+
let mem_ty = MemoryType::LOADER_DATA;
4646
let size = layout.size();
4747
let align = layout.align();
4848

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn allocate_pages(bt: &BootServices) {
1717
info!("Allocating some pages of memory");
1818

1919
let ty = AllocateType::AnyPages;
20-
let mem_ty = MemoryType::LoaderData;
20+
let mem_ty = MemoryType::LOADER_DATA;
2121
let pgs = bt
2222
.allocate_pages(ty, mem_ty, 1)
2323
.expect("Failed to allocate a page of memory");

0 commit comments

Comments
 (0)