From b12b4a77b65dc975dc182c2d9019bce38abc0394 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Tue, 12 Nov 2024 12:26:48 +0000 Subject: [PATCH] Use thiserror derive macro for MapError. --- CHANGELOG.md | 6 ++++++ Cargo.toml | 1 + src/lib.rs | 33 ++++++++------------------------- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a81a6f..d41c020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### New features + +- `MapError` now implements `core::error::Error`. + ## 0.8.0 ### Breaking changes diff --git a/Cargo.toml b/Cargo.toml index 18750bd..0a4b5a1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ categories = ["embedded", "no-std", "hardware-support"] [dependencies] bitflags = "2.6.0" +thiserror = { version = "2.0.3", default-features = false } zerocopy = { version = "0.8.2", features = ["derive"], optional = true } [features] diff --git a/src/lib.rs b/src/lib.rs index 842e8d5..6abb20d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,53 +59,36 @@ extern crate alloc; #[cfg(target_arch = "aarch64")] use core::arch::asm; -use core::fmt::{self, Display, Formatter}; use paging::{ Attributes, Constraints, Descriptor, MemoryRegion, PhysicalAddress, RootTable, Translation, TranslationRegime, VaRange, VirtualAddress, }; +use thiserror::Error; /// An error attempting to map some range in the page table. -#[derive(Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, Error, PartialEq)] pub enum MapError { /// The address requested to be mapped was out of the range supported by the page table /// configuration. + #[error("Virtual address {0} out of range")] AddressRange(VirtualAddress), /// The address requested to be mapped was not valid for the mapping in use. + #[error("Invalid virtual address {0} for mapping")] InvalidVirtualAddress(VirtualAddress), /// The end of the memory region is before the start. + #[error("End of memory region {0} is before start.")] RegionBackwards(MemoryRegion), /// There was an error while updating a page table entry. + #[error("Error updating page table entry {0:?}")] PteUpdateFault(Descriptor), /// The requested flags are not supported for this mapping + #[error("Flags {0:?} unsupported for mapping.")] InvalidFlags(Attributes), /// Updating the range violates break-before-make rules and the mapping is live + #[error("Cannot remap region {0} while translation is live.")] BreakBeforeMakeViolation(MemoryRegion), } -impl Display for MapError { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - match self { - Self::AddressRange(va) => write!(f, "Virtual address {} out of range", va), - Self::InvalidVirtualAddress(va) => { - write!(f, "Invalid virtual address {} for mapping", va) - } - Self::RegionBackwards(region) => { - write!(f, "End of memory region {} is before start.", region) - } - Self::PteUpdateFault(desc) => { - write!(f, "Error updating page table entry {:?}", desc) - } - Self::InvalidFlags(flags) => { - write!(f, "Flags {flags:?} unsupported for mapping.") - } - Self::BreakBeforeMakeViolation(region) => { - write!(f, "Cannot remap region {region} while translation is live.") - } - } - } -} - /// Manages a level 1 page table and associated state. /// /// Mappings should be added with [`map_range`](Self::map_range) before calling