use core::ops::{Index, IndexMut, RangeFrom};
use ctx::{FromCtx, IntoCtx};
pub trait Cread<Ctx, I = usize> : Index<I> + Index<RangeFrom<I>>
where
Ctx: Copy,
{
#[inline]
fn cread_with<'a, N: FromCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(&'a self, offset: I, ctx: Ctx) -> N {
N::from_ctx(&self[offset..], ctx)
}
#[inline]
fn cread<'a, N: FromCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(&'a self, offset: I) -> N where Ctx: Default {
let ctx = Ctx::default();
N::from_ctx(&self[offset..], ctx)
}
}
impl<Ctx: Copy, I, R: ?Sized + Index<I> + Index<RangeFrom<I>>> Cread<Ctx, I> for R {}
pub trait Cwrite<Ctx: Copy, I = usize>: Index<I> + IndexMut<RangeFrom<I>> {
#[inline]
fn cwrite<N: IntoCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(&mut self, n: N, offset: I) where Ctx: Default {
let ctx = Ctx::default();
n.into_ctx(self.index_mut(offset..), ctx)
}
#[inline]
fn cwrite_with<N: IntoCtx<Ctx, <Self as Index<RangeFrom<I>>>::Output>>(&mut self, n: N, offset: I, ctx: Ctx) {
n.into_ctx(self.index_mut(offset..), ctx)
}
}
impl<Ctx: Copy, I, W: ?Sized + Index<I> + IndexMut<RangeFrom<I>>> Cwrite<Ctx, I> for W {}