Skip to main content

ipld_car/bounded_reader/
traits.rs

1use crate::bounded_reader::error::BoundedReaderErr;
2
3use std::ops::Range;
4
5pub trait Bounded {
6	/// Returns the absolute range this bounded reader is restricted to.
7	fn bounds(&self) -> Range<u64>;
8
9	/// Returns the length of the bounded range.
10	fn bound_len(&self) -> u64;
11
12	/// Creates a new bounded reader that is a sub-range of this one.
13	fn sub<R: BoundedIndex<Self>>(&self, range: R) -> Result<Self, BoundedReaderErr>
14	where
15		Self: Sized;
16
17	fn clamped_sub<R: BoundedIndex<Self>>(&self, range: R) -> Self
18	where
19		Self: Sized;
20}
21
22/// Trait for types that can be used as sub-ranges of a bounded reader.
23pub trait BoundedIndex<T> {
24	fn get(self, bounded: &T) -> Result<T, BoundedReaderErr>;
25	fn clamped_get(self, bounded: &T) -> T;
26}
27
28pub trait CloneAndRewind {
29	/// Clones this bounded reader and resets the read position to the start of the range.
30	fn clone_and_rewind(&self) -> Self;
31}