use crate::{no_panic, Reader};
#[derive(Clone, Copy, Debug, Eq)]
pub struct Input<'a> {
value: no_panic::Slice<'a>,
}
impl<'a> Input<'a> {
pub const fn from(bytes: &'a [u8]) -> Self {
Self {
value: no_panic::Slice::new(bytes),
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.value.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.value.len()
}
pub fn read_all<F, R, E>(&self, incomplete_read: E, read: F) -> Result<R, E>
where
F: FnOnce(&mut Reader<'a>) -> Result<R, E>,
{
let mut input = Reader::new(*self);
let result = read(&mut input)?;
if input.at_end() {
Ok(result)
} else {
Err(incomplete_read)
}
}
#[inline]
pub fn as_slice_less_safe(&self) -> &'a [u8] {
self.value.as_slice_less_safe()
}
pub(super) fn into_value(self) -> no_panic::Slice<'a> {
self.value
}
}
impl<'a> From<&'a [u8]> for Input<'a> {
#[inline]
fn from(value: &'a [u8]) -> Self {
no_panic::Slice::new(value).into()
}
}
impl<'a> From<no_panic::Slice<'a>> for Input<'a> {
#[inline]
fn from(value: no_panic::Slice<'a>) -> Self {
Self { value }
}
}
impl PartialEq<Input<'_>> for Input<'_> {
#[inline]
fn eq(&self, other: &Input) -> bool {
self.as_slice_less_safe() == other.as_slice_less_safe()
}
}
impl PartialEq<[u8]> for Input<'_> {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
self.as_slice_less_safe() == other
}
}
impl PartialEq<Input<'_>> for [u8] {
#[inline]
fn eq(&self, other: &Input) -> bool {
other.as_slice_less_safe() == self
}
}