use core::marker::PhantomData;
use crate::buf::Cursor;
use crate::error::Error;
use crate::traits::ZeroCopy;
#[must_use = "Must call `Validator::end` when validation is completed"]
pub struct Validator<'a, T> {
cursor: Cursor<'a>,
_marker: PhantomData<T>,
}
impl<'a, T> Validator<'a, T> {
#[inline]
pub(crate) fn new(cursor: Cursor<'a>) -> Self
where
T: ZeroCopy,
{
Self {
cursor,
_marker: PhantomData,
}
}
#[inline]
pub fn field<F>(&mut self) -> Result<&F, Error>
where
F: ZeroCopy,
{
unsafe {
self.cursor.align::<F>();
F::validate(self.cursor)?;
};
Ok(unsafe { self.cursor.cast() })
}
}