rft/window/
range.rs

1use std::ops;
2
3/// Trait for a window range argument.
4pub trait Range: Clone {
5	/// The start of the range, if any.
6	fn start(&self) -> Option<u32> {
7		None
8	}
9
10	/// The end of the range, if any.
11	fn end(&self) -> Option<u32> {
12		None
13	}
14
15	/// The total width of the range.
16	fn width(&self, size: usize) -> u32 {
17		self.end().unwrap_or(size as u32) - self.start().unwrap_or(0)
18	}
19
20	/// Checks if the range is valid for the given size.
21	fn is_valid(&self, size: usize) -> bool {
22		if let Some(end) = self.end() {
23			return end as usize <= size;
24		}
25
26		true
27	}
28}
29
30impl Range for ops::Range<u32> {
31	fn start(&self) -> Option<u32> {
32		Some(self.start)
33	}
34
35	fn end(&self) -> Option<u32> {
36		Some(self.end)
37	}
38}
39
40impl Range for ops::RangeTo<u32> {
41	fn end(&self) -> Option<u32> {
42		Some(self.end)
43	}
44}
45
46impl Range for ops::RangeFrom<u32> {
47	fn start(&self) -> Option<u32> {
48		Some(self.start)
49	}
50}
51
52impl Range for ops::RangeFull { }