use core::ops::Range;
#[derive(Debug)]
pub struct Segment {
pub index: usize,
pub len: usize,
pub capacity: usize,
}
impl Segment {
pub fn index(&self, other: &Self) -> Option<usize> {
if self.index == other.index && self.len == 0 {
None
} else {
Some(self.index)
}
}
pub fn range(&self) -> Range<usize> {
let index = self.index;
index..(index + self.len)
}
pub fn move_into(&mut self, other: &mut Self, count: usize) {
debug_assert!(
count <= self.len,
"cannot move more messages than {}, tried to move {}",
self.len,
count
);
let index = self.index + count;
self.index = if let Some(index) = index.checked_sub(self.capacity) {
index
} else {
index
};
self.len -= count;
other.len += count;
}
}