use std::fmt;
use std::ops::Range;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Span {
pub start: usize,
pub end: usize,
pub rev: u64,
}
impl Span {
pub fn new(start: usize, end: usize, rev: u64) -> Self {
debug_assert!(start <= end, "span start must not exceed its end");
Self { start, end, rev }
}
pub fn len(&self) -> usize {
self.end - self.start
}
pub fn is_empty(&self) -> bool {
self.start == self.end
}
pub fn contains(&self, other: &Span) -> bool {
self.start <= other.start && other.end <= self.end
}
pub fn contains_offset(&self, offset: usize) -> bool {
self.start <= offset && offset < self.end
}
pub fn overlaps(&self, other: &Span) -> bool {
self.start < other.end && other.start < self.end
}
pub fn to_range(&self) -> Range<usize> {
self.start..self.end
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}..{}@{}", self.start, self.end, self.rev)
}
}