56 lines
1.6 KiB
Rust
56 lines
1.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
|
|
/// Domain, in sectors.
|
|
/// Requires sector_size to be provided elsewhere for conversion to bytes.
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
|
|
pub struct Domain {
|
|
pub start: usize,
|
|
pub end: usize,
|
|
}
|
|
|
|
impl Default for Domain {
|
|
fn default() -> Self {
|
|
Domain { start: 0, end: 1 }
|
|
}
|
|
}
|
|
|
|
impl Domain {
|
|
/// Return length of domain in sectors.
|
|
pub fn len(self) -> usize {
|
|
self.end - self.start
|
|
}
|
|
|
|
/// Returns the type of overlap between this domain and another.
|
|
pub fn overlap(&self, other: Domain) -> DomainOverlap {
|
|
if self.end <= other.start || other.end <= self.start {
|
|
// Cases 7, 8, 12, and 13 of map::tests::test_update
|
|
DomainOverlap::None
|
|
} else if other.start >= self.start && other.end <= self.end {
|
|
// Cases 3, 5, 9, and 11 of map::tests::test_update
|
|
DomainOverlap::SelfEngulfsOther
|
|
} else if other.start <= self.start && other.end >= self.end {
|
|
// Cases 4, 6, and 10 of map::tests::test_update
|
|
DomainOverlap::OtherEngulfsSelf
|
|
} else if self.start < other.start {
|
|
// Case 1 of map::tests::test_update
|
|
DomainOverlap::OtherOverlapsEnd
|
|
} else {
|
|
// Case 2 of map::tests::test_update
|
|
DomainOverlap::OtherOverlapsStart
|
|
}
|
|
}
|
|
}
|
|
|
|
pub enum DomainOverlap {
|
|
None,
|
|
SelfEngulfsOther,
|
|
OtherEngulfsSelf,
|
|
OtherOverlapsStart,
|
|
OtherOverlapsEnd,
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
}
|