use crate::{no_panic, Reader};
#[derive(Clone, Copy)]
pub struct Input<'a> {
value: no_panic::Slice<'a>,
}
impl core::fmt::Debug for Input<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Input").finish()
}
}
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 }
}
}