musli-zerocopy 0.0.59

Zero copy primitives for use in Müsli.
Documentation
use crate::buf::Buf;
use crate::error::Error;
use crate::load::Load;

/// Trait used for handling any kind of zero copy value, be they references or
/// not.
pub trait Visit {
    /// The target being read.
    type Target: ?Sized;

    /// Validate the value.
    fn visit<V, O>(&self, buf: &Buf, visitor: V) -> Result<O, Error>
    where
        V: FnOnce(&Self::Target) -> O;
}

impl<T: ?Sized> Visit for T
where
    T: Load,
{
    type Target = T::Target;

    fn visit<V, O>(&self, buf: &Buf, visitor: V) -> Result<O, Error>
    where
        V: FnOnce(&Self::Target) -> O,
    {
        let value = buf.load(self)?;
        Ok(visitor(value))
    }
}