use crate::error::{Error, ErrorKind};
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct SizeHint {
lower: u64,
upper: Option<u64>,
}
impl SizeHint {
#[inline]
pub const fn new() -> Self {
Self { lower: 0, upper: None }
}
#[inline]
pub const fn with_exact(bytes: u64) -> Self {
Self {
lower: bytes,
upper: Some(bytes),
}
}
pub fn with_bounds(lower: u64, upper: Option<u64>) -> Result<Self, Error> {
if upper.is_some_and(|upper| upper < lower) {
return Err(Error::new(
ErrorKind::LocalMessage,
"body size upper bound is below its lower bound",
));
}
Ok(Self { lower, upper })
}
#[inline]
pub const fn lower(&self) -> u64 {
self.lower
}
#[inline]
pub const fn upper(&self) -> Option<u64> {
self.upper
}
#[inline]
pub const fn exact(&self) -> Option<u64> {
match self.upper {
Some(upper) if upper == self.lower => Some(upper),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::SizeHint;
#[test]
fn bounds_distinguish_unknown_exact_and_invalid_lengths() {
for (lower, upper, exact) in [
(0, None, None),
(0, Some(0), Some(0)),
(4, Some(9), None),
(u64::MAX, Some(u64::MAX), Some(u64::MAX)),
] {
assert_eq!(SizeHint::with_bounds(lower, upper).unwrap().exact(), exact);
}
assert!(SizeHint::with_bounds(1, Some(0)).is_err());
}
}