Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description = "Library with types for handling PCI devices"
categories = ["hardware-support", "no-std"]
readme = "README.md"
license = "MIT/Apache-2.0"
edition = "2018"
edition = "2021"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2021 edition has TryInto in the prelude. If preferred I can manually import it instead.


[dependencies]
bit_field = "0.10"
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,28 @@ pub enum Bar {
Io { port: u32 },
}

impl Bar {
/// Return the IO port of this BAR or panic if not an IO BAR.
pub fn unwrap_io(self) -> u32 {
match self {
Bar::Io { port } => port,
Bar::Memory32 { .. } | Bar::Memory64 { .. } => panic!("expected IO BAR, found memory BAR"),
}
}

/// Return the address and size of this BAR or panic if not a memory BAR.
pub fn unwrap_mem(self) -> (usize, usize) {
match self {
Bar::Memory32 { address, size, prefetchable: _ } => (address as usize, size as usize),
Bar::Memory64 { address, size, prefetchable: _ } => (
address.try_into().expect("conversion from 64bit BAR to usize failed"),
size.try_into().expect("conversion from 64bit BAR to usize failed"),
),
Bar::Io { .. } => panic!("expected memory BAR, found IO BAR"),
}
}
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum BarWriteError {
NoSuchBar,
Expand Down