Commit 31f8162
committed
Make
Allows users of the crate to have their own Read implementations
and create the equivalent of the `from_*` functions.
Specifically, if the bytes to read are actually a continuous
stream of bencoded data (so a dictionary followed by another
dictionary for instance), then the library user can create
a reader which does not check that it reached the end
of all the bytes after reading one value.
For instance:
```
use std::{cell::RefCell, rc::Rc};
pub(crate) struct SliceReadView<'a> {
reader: Rc<RefCell<bt_bencode::read::SliceRead<'a>>>,
}
impl<'a> SliceReadView<'a> {
pub(crate) fn new(reader: Rc<RefCell<bt_bencode::read::SliceRead<'a>>>) -> Self {
Self { reader }
}
}
impl<'a> bt_bencode::read::Read<'a> for SliceReadView<'a> {
#[inline]
fn next(&mut self) -> Option<Result<u8, bt_bencode::Error>> {
self.reader.borrow_mut().next()
}
#[inline]
fn peek(&mut self) -> Option<Result<u8, bt_bencode::Error>> {
self.reader.borrow_mut().peek()
}
#[inline]
fn byte_offset(&self) -> usize {
self.reader.borrow().byte_offset()
}
}
```
To read multiple values:
```
let reader = std::rc::Rc::new(std::cell::RefCell::new(bt_bencode::read::SliceRead::new(
&bytes[..],
)));
loop {
let mut de = bt_bencode::Deserializer::new(SliceReadView::new(reader.clone()));
use bt_bencode::read::Read;
use serde::de::Deserialize;
match bt_bencode::Value::deserialize(&mut de) {
Ok(value) => {
// Process the decoded `value`
}
Err(e) => match e {
bt_bencode::Error::EofWhileParsingValue
if reader.clone().borrow_mut().peek().is_none() =>
{
// The end of file was reached as expected
break;
}
_ => {
// An unexpected error occured during processing
dbg!(e);
}
},
}
}
```Read trait and helpers public1 parent bb15790 commit 31f8162
3 files changed
+21
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
3 | 9 | | |
4 | 10 | | |
5 | 11 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| 10 | + | |
8 | 11 | | |
| 12 | + | |
9 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
10 | 17 | | |
| 18 | + | |
11 | 19 | | |
12 | 20 | | |
13 | 21 | | |
| 22 | + | |
14 | 23 | | |
15 | | - | |
| 24 | + | |
16 | 25 | | |
17 | 26 | | |
18 | 27 | | |
| |||
26 | 35 | | |
27 | 36 | | |
28 | 37 | | |
| 38 | + | |
29 | 39 | | |
30 | 40 | | |
31 | 41 | | |
| |||
79 | 89 | | |
80 | 90 | | |
81 | 91 | | |
82 | | - | |
| 92 | + | |
| 93 | + | |
83 | 94 | | |
84 | 95 | | |
85 | 96 | | |
86 | 97 | | |
87 | 98 | | |
| 99 | + | |
88 | 100 | | |
89 | 101 | | |
90 | 102 | | |
| |||
0 commit comments