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
47
48

use std::fmt;

/// Get's returned when there is not enough data left to seek to the position.
///
/// The max position get's returned.
#[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 {}


/// Sets the internal position for writing or reading.
pub trait BytesSeek {

	/// Returns the internal position.
	fn position(&self) -> usize;

	/// Sets the internal position if possible.
	fn try_seek(&mut self, pos: usize) -> Result<(), SeekError>;

	/// Sets the internal position.
	#[track_caller]
	fn seek(&mut self, pos: usize) {
		self.try_seek(pos).expect("failed to seek");
	}

	/// Advances the internal position if possible.
	fn try_advance(&mut self, adv: usize) -> Result<(), SeekError> {
		self.try_seek(self.position() + adv)
	}

	/// Advances the internal position.
	/// 
	/// ## Panic
	/// May panic depending on the `BytesSeek::seek` implementation.
	#[track_caller]
	fn advance(&mut self, adv: usize) {
		self.try_advance(adv).expect("failed to advance")
	}

}