mirror of
https://github.com/Cuprate/cuprate.git
synced 2025-03-15 16:32:23 +00:00
Some checks failed
Audit / audit (push) Has been cancelled
CI / fmt (push) Has been cancelled
CI / typo (push) Has been cancelled
CI / ci (macos-latest, stable, bash) (push) Has been cancelled
CI / ci (ubuntu-latest, stable, bash) (push) Has been cancelled
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Has been cancelled
Deny / audit (push) Has been cancelled
Doc / build (push) Has been cancelled
Doc / deploy (push) Has been cancelled
44 lines
913 B
Rust
44 lines
913 B
Rust
use bytes::{Buf, BufMut};
|
|
|
|
use crate::error::*;
|
|
|
|
#[inline]
|
|
pub fn checked_read_primitive<B: Buf, R: Sized>(
|
|
b: &mut B,
|
|
read: impl Fn(&mut B) -> R,
|
|
) -> Result<R> {
|
|
checked_read(b, read, size_of::<R>())
|
|
}
|
|
|
|
#[inline]
|
|
pub fn checked_read<B: Buf, R>(b: &mut B, read: impl Fn(&mut B) -> R, size: usize) -> Result<R> {
|
|
if b.remaining() < size {
|
|
Err(Error::IO("Not enough bytes in buffer to build object."))?;
|
|
}
|
|
|
|
Ok(read(b))
|
|
}
|
|
|
|
#[inline]
|
|
pub fn checked_write_primitive<B: BufMut, T: Sized>(
|
|
b: &mut B,
|
|
write: impl Fn(&mut B, T),
|
|
t: T,
|
|
) -> Result<()> {
|
|
checked_write(b, write, t, size_of::<T>())
|
|
}
|
|
|
|
#[inline]
|
|
pub fn checked_write<B: BufMut, T>(
|
|
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."))?;
|
|
}
|
|
|
|
write(b, t);
|
|
Ok(())
|
|
}
|