simple_bytes/
bytes_seek.rs1
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct SeekError(pub usize);
9
10impl fmt::Display for SeekError {
11 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
12 fmt::Debug::fmt(self, f)
13 }
14}
15
16impl std::error::Error for SeekError {}
17
18
19pub trait BytesSeek {
21 fn position(&self) -> usize;
23
24 fn try_seek(&mut self, pos: usize) -> Result<(), SeekError>;
26
27 #[track_caller]
29 fn seek(&mut self, pos: usize) {
30 self.try_seek(pos).expect("failed to seek");
31 }
32
33 fn try_advance(&mut self, adv: usize) -> Result<(), SeekError> {
35 self.try_seek(self.position() + adv)
36 }
37
38 #[track_caller]
43 fn advance(&mut self, adv: usize) {
44 self.try_advance(adv).expect("failed to advance")
45 }
46}
47
48impl<S: BytesSeek> BytesSeek for &mut S {
49 #[inline]
50 fn position(&self) -> usize {
51 (**self).position()
52 }
53
54 #[inline]
55 fn try_seek(&mut self, pos: usize) -> Result<(), SeekError> {
56 (**self).try_seek(pos)
57 }
58}