1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SeekError(pub usize);
impl fmt::Display for SeekError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
impl std::error::Error for SeekError {}
pub trait BytesSeek {
fn position(&self) -> usize;
fn try_seek(&mut self, pos: usize) -> Result<(), SeekError>;
fn seek(&mut self, pos: usize) {
self.try_seek(pos).expect("failed to seek");
}
fn try_advance(&mut self, adv: usize) -> Result<(), SeekError> {
self.try_seek(self.position() + adv)
}
fn advance(&mut self, adv: usize) {
self.try_advance(adv).expect("failed to advance")
}
}