use bytes::{Buf, BufMut}; use crate::error::*; #[inline] pub(crate) fn checked_read_primitive( b: &mut B, read: impl Fn(&mut B) -> R, ) -> Result { checked_read(b, read, size_of::()) } #[inline] pub(crate) fn checked_read( b: &mut B, read: impl Fn(&mut B) -> R, size: usize, ) -> Result { if b.remaining() < size { Err(Error::IO("Not enough bytes in buffer to build object.")) } else { Ok(read(b)) } } #[inline] pub(crate) fn checked_write_primitive( b: &mut B, write: impl Fn(&mut B, T), t: T, ) -> Result<()> { checked_write(b, write, t, size_of::()) } #[inline] pub(crate) fn checked_write( b: &mut B, write: impl Fn(&mut B, T), t: T, size: usize, ) -> Result<()> { if b.remaining_mut() < size { Err(Error::IO("Not enough capacity to write object.")) } else { write(b, t); Ok(()) } }