Skip to content

Commit bc29601

Browse files
authored
Merge pull request #63 from google/thiserror
Use thiserror derive macro for MapError.
2 parents df5fa16 + b12b4a7 commit bc29601

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### New features
6+
7+
- `MapError` now implements `core::error::Error`.
8+
39
## 0.8.0
410

511
### Breaking changes

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ categories = ["embedded", "no-std", "hardware-support"]
1414

1515
[dependencies]
1616
bitflags = "2.6.0"
17+
thiserror = { version = "2.0.3", default-features = false }
1718
zerocopy = { version = "0.8.2", features = ["derive"], optional = true }
1819

1920
[features]

src/lib.rs

+8-25
Original file line numberDiff line numberDiff line change
@@ -59,53 +59,36 @@ extern crate alloc;
5959

6060
#[cfg(target_arch = "aarch64")]
6161
use core::arch::asm;
62-
use core::fmt::{self, Display, Formatter};
6362
use paging::{
6463
Attributes, Constraints, Descriptor, MemoryRegion, PhysicalAddress, RootTable, Translation,
6564
TranslationRegime, VaRange, VirtualAddress,
6665
};
66+
use thiserror::Error;
6767

6868
/// An error attempting to map some range in the page table.
69-
#[derive(Clone, Debug, Eq, PartialEq)]
69+
#[derive(Clone, Debug, Eq, Error, PartialEq)]
7070
pub enum MapError {
7171
/// The address requested to be mapped was out of the range supported by the page table
7272
/// configuration.
73+
#[error("Virtual address {0} out of range")]
7374
AddressRange(VirtualAddress),
7475
/// The address requested to be mapped was not valid for the mapping in use.
76+
#[error("Invalid virtual address {0} for mapping")]
7577
InvalidVirtualAddress(VirtualAddress),
7678
/// The end of the memory region is before the start.
79+
#[error("End of memory region {0} is before start.")]
7780
RegionBackwards(MemoryRegion),
7881
/// There was an error while updating a page table entry.
82+
#[error("Error updating page table entry {0:?}")]
7983
PteUpdateFault(Descriptor),
8084
/// The requested flags are not supported for this mapping
85+
#[error("Flags {0:?} unsupported for mapping.")]
8186
InvalidFlags(Attributes),
8287
/// Updating the range violates break-before-make rules and the mapping is live
88+
#[error("Cannot remap region {0} while translation is live.")]
8389
BreakBeforeMakeViolation(MemoryRegion),
8490
}
8591

86-
impl Display for MapError {
87-
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
88-
match self {
89-
Self::AddressRange(va) => write!(f, "Virtual address {} out of range", va),
90-
Self::InvalidVirtualAddress(va) => {
91-
write!(f, "Invalid virtual address {} for mapping", va)
92-
}
93-
Self::RegionBackwards(region) => {
94-
write!(f, "End of memory region {} is before start.", region)
95-
}
96-
Self::PteUpdateFault(desc) => {
97-
write!(f, "Error updating page table entry {:?}", desc)
98-
}
99-
Self::InvalidFlags(flags) => {
100-
write!(f, "Flags {flags:?} unsupported for mapping.")
101-
}
102-
Self::BreakBeforeMakeViolation(region) => {
103-
write!(f, "Cannot remap region {region} while translation is live.")
104-
}
105-
}
106-
}
107-
}
108-
10992
/// Manages a level 1 page table and associated state.
11093
///
11194
/// Mappings should be added with [`map_range`](Self::map_range) before calling

0 commit comments

Comments
 (0)