data_streams/source/markers.rs
1// Copyright 2024 - Strixpyrr
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::DataSource;
5
6/// A trait which marks a source as infinite, preventing "read-to-end" operations
7/// from completing.
8///
9/// # Safety
10///
11/// The source must be truly infinite; it must **always** produce data. An example
12/// in the standard library is [`std::io::Repeat`].
13pub unsafe trait InfiniteSource: DataSource { }
14
15/// A trait which gives known upper and lower bounds of the size of the source.
16///
17/// # Safety
18///
19/// Other functions rely on these bounds being correct. The source must produce at
20/// least the number of bytes returned by [`lower_bound`], and at most that returned
21/// by [`upper_bound`].
22///
23/// [`lower_bound`]: SourceSize::lower_bound
24/// [`upper_bound`]: SourceSize::upper_bound
25pub unsafe trait SourceSize {
26 fn lower_bound(&self) -> u64 { 0 }
27 fn upper_bound(&self) -> Option<u64> { None }
28}
29
30// Safety: infinite sources produce at least zero bytes and have no upper bound, by definition.
31unsafe impl<T: InfiniteSource> SourceSize for T { }