1use std::ops::Index;
2
3#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
5pub struct Span {
6 pub start: usize,
8 pub len: usize,
10}
11
12impl Span {
13 pub const fn new(start: usize, len: usize) -> Self {
15 Self { start, len }
16 }
17
18 pub const fn end(self) -> usize {
20 self.start + self.len
21 }
22
23 pub const fn with_offset(self, delta: usize) -> Self {
24 Span::new(self.start + delta, self.len)
25 }
26}
27
28impl Index<Span> for [u8] {
29 type Output = [u8];
30
31 #[track_caller]
32 fn index(&self, index: Span) -> &Self::Output {
33 &self[index.start..index.end()]
34 }
35}