use core::result;
use core::fmt::Debug;
use ctx::{TryFromCtx, TryRefFromCtx};
use error;
use endian::Endian;
pub trait Pread<Ctx = Endian, E = error::Error, I = usize, TryCtx = (I, Ctx), SliceCtx = (I, I, Ctx) >
where E: Debug,
Ctx: Copy + Default + Debug,
I: Copy + Debug,
TryCtx: Copy + Default + Debug,
SliceCtx: Copy + Default + Debug,
{
#[inline]
fn pread_unsafe<'a, N: TryFromCtx<'a, TryCtx, Error = E>>(&'a self, offset: I, ctx: Ctx) -> N {
self.pread_with(offset, ctx).unwrap()
}
#[inline]
fn pread<'a, N: TryFromCtx<'a, TryCtx, Error = E>>(&'a self, offset: I) -> result::Result<N, E> {
self.pread_with(offset, Ctx::default())
}
#[inline]
fn pread_with<'a, N: TryFromCtx<'a, TryCtx, Error = E>>(&'a self, offset: I, ctx: Ctx) -> result::Result<N, E>;
#[inline]
fn pread_slice<'a, N: ?Sized + TryRefFromCtx<SliceCtx, Error = E>>(&'a self, offset: I, count: I) -> result::Result<&'a N, E>;
}
impl<Ctx, E> Pread<Ctx, E> for [u8]
where
E: Debug,
Ctx: Debug + Copy + Default {
#[inline]
fn pread_unsafe<'a, N: TryFromCtx<'a, (usize, Ctx), Error = E>>(&'a self, offset: usize, le: Ctx) -> N {
TryFromCtx::try_from_ctx(self, (offset, le)).unwrap()
}
#[inline]
fn pread_with<'a, N: TryFromCtx<'a, (usize, Ctx), Error = E>>(&'a self, offset: usize, le: Ctx) -> result::Result<N, E> {
TryFromCtx::try_from_ctx(self, (offset, le))
}
#[inline]
fn pread_slice<N: ?Sized + TryRefFromCtx<(usize, usize, Ctx), Error = E>>(&self, offset: usize, count: usize) -> result::Result<&N, E> {
TryRefFromCtx::try_ref_from_ctx(self, (offset, count, Ctx::default()))
}
}
impl<Ctx, E, T> Pread<Ctx, E> for T
where
E: Debug,
Ctx: Debug + Copy + Default,
T: AsRef<[u8]> {
#[inline]
fn pread_unsafe<'a, N: TryFromCtx<'a, (usize, Ctx), Error = E>>(&'a self, offset: usize, le: Ctx) -> N {
<[u8] as Pread<Ctx, E>>::pread_unsafe::<N>(self.as_ref(), offset, le)
}
#[inline]
fn pread_with<'a, N: TryFromCtx<'a, (usize, Ctx), Error = E>>(&'a self, offset: usize, le: Ctx) -> result::Result<N, E> {
TryFromCtx::try_from_ctx(self.as_ref(), (offset, le))
}
#[inline]
fn pread_slice<N: ?Sized + TryRefFromCtx<(usize, usize, Ctx), Error = E>>(&self, offset: usize, count: usize) -> result::Result<&N, E> {
<[u8] as Pread<Ctx, E>>::pread_slice::<N>(self.as_ref(), offset, count)
}
}