use std::io::{Cursor, Read, Write};
#[cfg(feature = "verbose")]
pub use log::debug;
#[doc(hidden)]
pub use static_assertions as sa;
#[cfg(test)]
mod tests;
mod error;
pub use error::*;
mod default_impls;
pub use default_impls::*;
mod helpers;
pub use helpers::*;
pub use simple_parse_derive::*;
pub struct SpCtx {
pub cursor: usize,
pub is_reading: bool,
pub is_little_endian: bool,
pub count: Option<usize>,
}
impl Default for SpCtx {
fn default() -> Self {
Self {
cursor: 0,
is_reading: true,
is_little_endian: cfg!(target_endian = "little"),
count: None,
}
}
}
#[doc(hidden)]
pub type DefaultCountType = u32;
#[doc(hidden)]
pub const MAX_ALLOC_SIZE: usize = 4096;
pub unsafe trait SpOptHints {
const IS_VAR_SIZE: bool = true;
const STATIC_SIZE: usize = 0;
const COUNT_SIZE: usize = 0;
}
pub trait SpRead {
fn inner_from_reader<R: Read + ?Sized>(
src: &mut R,
ctx: &mut SpCtx,
) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints;
unsafe fn inner_from_reader_unchecked<R: Read + ?Sized>(
checked_bytes: *mut u8,
src: &mut R,
ctx: &mut SpCtx,
) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints;
fn from_reader<R: Read + ?Sized>(src: &mut R) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints,
{
Self::inner_from_reader(src, &mut SpCtx::default())
}
}
pub trait SpReadRaw<'b> {
fn inner_from_slice(
src: &mut Cursor<&'b [u8]>,
ctx: &mut SpCtx,
) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints;
unsafe fn inner_from_slice_unchecked(
checked_bytes: *const u8,
src: &mut Cursor<&'b [u8]>,
ctx: &mut SpCtx,
) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints;
fn from_slice(src: &mut Cursor<&'b [u8]>) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints,
{
Self::inner_from_slice(src, &mut SpCtx::default())
}
}
pub trait SpReadRawMut<'b> {
fn inner_from_mut_slice(
src: &mut Cursor<&'b mut [u8]>,
ctx: &mut SpCtx,
) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints;
unsafe fn inner_from_mut_slice_unchecked(
checked_bytes: *mut u8,
src: &mut Cursor<&'b mut [u8]>,
ctx: &mut SpCtx,
) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints;
fn from_mut_slice(src: &mut Cursor<&'b mut [u8]>) -> Result<Self, crate::SpError>
where
Self: Sized + SpOptHints,
{
Self::inner_from_mut_slice(src, &mut SpCtx::default())
}
}
pub trait SpWrite {
fn inner_to_writer<W: Write + ?Sized>(
&self,
ctx: &mut SpCtx,
dst: &mut W,
) -> Result<usize, crate::SpError>;
fn to_writer<W: Write + ?Sized>(&self, dst: &mut W) -> Result<usize, crate::SpError> {
self.inner_to_writer(&mut SpCtx::default(), dst)
}
}