Skip to content

Commit 0969c9c

Browse files
committed
drm/asahi: util: Add RangeExt helpers for Range<T>
Signed-off-by: Asahi Lina <lina@asahilina.net>
1 parent e56bf25 commit 0969c9c

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

drivers/gpu/drm/asahi/util.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,34 @@ where
4242

4343
(a + b - one) / b
4444
}
45+
46+
pub(crate) trait RangeExt<T> {
47+
fn overlaps(&self, other: Self) -> bool;
48+
fn is_superset(&self, other: Self) -> bool;
49+
fn len(&self) -> usize;
50+
fn range(&self) -> T;
51+
}
52+
53+
impl<T: PartialOrd<T> + Default + Copy + Sub<Output = T>> RangeExt<T> for core::ops::Range<T>
54+
where
55+
usize: core::convert::TryFrom<T>,
56+
<usize as core::convert::TryFrom<T>>::Error: core::fmt::Debug,
57+
{
58+
fn overlaps(&self, other: Self) -> bool {
59+
!(self.is_empty() || other.is_empty() || self.end <= other.start || other.end <= self.start)
60+
}
61+
fn is_superset(&self, other: Self) -> bool {
62+
!self.is_empty()
63+
&& (other.is_empty() || (other.start >= self.start && other.end <= self.end))
64+
}
65+
fn range(&self) -> T {
66+
if self.is_empty() {
67+
Default::default()
68+
} else {
69+
self.end - self.start
70+
}
71+
}
72+
fn len(&self) -> usize {
73+
self.range().try_into().unwrap()
74+
}
75+
}

0 commit comments

Comments
 (0)