Debian Codegraph @

helix-core/src/rope_reader.rs

raw symbols SCIP
1 use std::io;
2
3 use ropey::iter::Chunks;
4 use ropey::RopeSlice;
5
6 pub struct RopeReader<'a> {
7 current_chunk: &'a [u8],
8 chunks: Chunks<'a>,
9 }
10
11 impl<'a> RopeReader<'a> {
12 pub fn new(rope: RopeSlice<'a>) -> RopeReader<'a> {
13 RopeReader {
14 current_chunk: &[],
15 chunks: rope.chunks(),
16 }
17 }
18 }
19
20 impl io::Read for RopeReader<'_> {
21 fn read(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
22 let buf_len = buf.len();
23 loop {
24 let read_bytes = self.current_chunk.read(buf)?;
25 buf = &mut buf[read_bytes..];
26 if buf.is_empty() {
27 return Ok(buf_len);
28 }
29
30 if let Some(next_chunk) = self.chunks.next() {
31 self.current_chunk = next_chunk.as_bytes();
32 } else {
33 return Ok(buf_len - buf.len());
34 }
35 }
36 }
37 }