Skip to main content

game_toolkit_dosbus/
port.rs

1/// A nonempty inclusive range of 16-bit I/O ports.
2#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
3pub struct PortRange {
4    start: u16,
5    end: u16,
6}
7
8impl PortRange {
9    /// Construct an inclusive range, usable in static device port maps.
10    ///
11    /// # Panics
12    /// Panics if `start > end`.
13    pub const fn new(start: u16, end: u16) -> Self {
14        assert!(start <= end, "reversed I/O port range");
15        Self { start, end }
16    }
17
18    /// Construct a range for exactly one port.
19    pub const fn single(port: u16) -> Self {
20        Self::new(port, port)
21    }
22
23    /// First port in the range, inclusive.
24    pub const fn start(self) -> u16 {
25        self.start
26    }
27
28    /// Last port in the range, inclusive.
29    pub const fn end(self) -> u16 {
30        self.end
31    }
32
33    /// Whether this range includes `port`.
34    pub const fn contains(self, port: u16) -> bool {
35        self.start <= port && port <= self.end
36    }
37
38    /// Whether this range and `other` share any port.
39    pub const fn overlaps(self, other: Self) -> bool {
40        self.start <= other.end && other.start <= self.end
41    }
42}