#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
#[must_use]
pub const fn new(start: usize, end: usize) -> Self {
Self { start, end }
}
#[must_use]
pub const fn empty(at: usize) -> Self {
Self { start: at, end: at }
}
#[must_use]
pub const fn len(&self) -> usize {
self.end.saturating_sub(self.start)
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn len_and_empty() {
assert_eq!(Span::new(2, 5).len(), 3);
assert!(Span::empty(4).is_empty());
assert_eq!(Span::empty(4), Span::new(4, 4));
assert_eq!(Span::new(5, 2).len(), 0);
assert!(!Span::new(0, 1).is_empty());
}
}