cargo_show_asm/
cached_lines.rs1use line_span::LineSpans;
2use std::ops::{Index, Range};
3
4pub struct CachedLines {
5 pub content: String,
6 pub splits: Vec<Range<usize>>,
7}
8
9impl CachedLines {
10 #[must_use]
11 pub fn without_ending(content: String) -> Self {
12 let splits = content.line_spans().map(|s| s.range()).collect::<Vec<_>>();
13 Self { content, splits }
14 }
15
16 #[must_use]
17 pub fn iter(&self) -> LineIter<'_> {
18 LineIter {
19 payload: self,
20 current: 0,
21 }
22 }
23 #[must_use]
24 pub fn get(&self, index: usize) -> Option<&str> {
25 let range = self.splits.get(index)?.clone();
26 Some(&self.content[range])
27 }
28}
29
30impl Index<usize> for CachedLines {
31 type Output = str;
32
33 fn index(&self, index: usize) -> &Self::Output {
34 &self.content[self.splits[index].clone()]
35 }
36}
37
38pub struct LineIter<'a> {
39 payload: &'a CachedLines,
40 current: usize,
41}
42
43impl<'a> IntoIterator for &'a CachedLines {
44 type Item = &'a str;
45
46 type IntoIter = LineIter<'a>;
47
48 fn into_iter(self) -> Self::IntoIter {
49 self.iter()
50 }
51}
52
53impl<'a> Iterator for LineIter<'a> {
54 type Item = &'a str;
55
56 fn next(&mut self) -> Option<Self::Item> {
57 self.current += 1;
58 self.payload.get(self.current - 1)
59 }
60}