use crate::decode::Decoder;
use crate::encode::Encoder;
use crate::error::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RawValue<'a>(&'a [u8]);
impl<'a> RawValue<'a> {
#[inline(always)]
#[must_use]
pub const fn as_bytes(&self) -> &'a [u8] {
self.0
}
#[inline(always)]
#[must_use]
pub const fn len(&self) -> usize {
self.0.len()
}
#[inline(always)]
#[must_use]
pub const fn is_empty(&self) -> bool {
false
}
#[inline(always)]
#[must_use]
pub fn decoder(&self) -> Decoder<'a> {
Decoder::new(self.0)
}
}
impl core::ops::Deref for RawValue<'_> {
type Target = [u8];
fn deref(&self) -> &[u8] {
self.0
}
}
impl Encoder<'_> {
pub fn raw(&mut self, value: &RawValue<'_>) -> Result<&mut Self, Error> {
let Some(end) = self.pos.checked_add(value.len()) else {
return Err(Error::OUT_OF_SPACE);
};
if end > self.buf.len() {
return Err(Error::OUT_OF_SPACE);
}
self.buf[self.pos..end].copy_from_slice(value.as_bytes());
self.pos = end;
Ok(self)
}
}
impl Decoder<'_> {
pub fn raw_value(&mut self) -> Result<RawValue<'_>, Error> {
let start = self.pos;
self.skip()?;
Ok(RawValue(&self.input[start..self.pos]))
}
}