Skip to content

Commit 2c1c472

Browse files
bjorn3IsaacWoods
authored andcommitted
Add unwrap_io and unwrap_mem convenience methods on Bar
This makes PCI drivers slightly easier to write. Redox OS has the same methods on it's counterpart to the Bar type of this crate.
1 parent 8c5a215 commit 2c1c472

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ description = "Library with types for handling PCI devices"
77
categories = ["hardware-support", "no-std"]
88
readme = "README.md"
99
license = "MIT/Apache-2.0"
10-
edition = "2018"
10+
edition = "2021"
1111

1212
[dependencies]
1313
bit_field = "0.10"

src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,28 @@ pub enum Bar {
549549
Io { port: u32 },
550550
}
551551

552+
impl Bar {
553+
/// Return the IO port of this BAR or panic if not an IO BAR.
554+
pub fn unwrap_io(self) -> u32 {
555+
match self {
556+
Bar::Io { port } => port,
557+
Bar::Memory32 { .. } | Bar::Memory64 { .. } => panic!("expected IO BAR, found memory BAR"),
558+
}
559+
}
560+
561+
/// Return the address and size of this BAR or panic if not a memory BAR.
562+
pub fn unwrap_mem(self) -> (usize, usize) {
563+
match self {
564+
Bar::Memory32 { address, size, prefetchable: _ } => (address as usize, size as usize),
565+
Bar::Memory64 { address, size, prefetchable: _ } => (
566+
address.try_into().expect("conversion from 64bit BAR to usize failed"),
567+
size.try_into().expect("conversion from 64bit BAR to usize failed"),
568+
),
569+
Bar::Io { .. } => panic!("expected memory BAR, found IO BAR"),
570+
}
571+
}
572+
}
573+
552574
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
553575
pub enum BarWriteError {
554576
NoSuchBar,

0 commit comments

Comments
 (0)