use super::{Allocator, Commit, Committer};
use crate::Result;
use core::borrow::Borrow;
use core::iter::once;
use core::marker::PhantomData;
use core::ptr::NonNull;
#[derive(Debug, PartialEq, Eq)]
pub struct InRef<'a, T: ?Sized> {
pub(super) ptr: NonNull<T>,
pub(super) offset: usize,
phantom: PhantomData<&'a mut T>,
}
impl<'a, T: ?Sized> InRef<'a, T> {
#[inline]
pub(super) fn new(ptr: NonNull<T>, offset: usize) -> Self {
Self {
ptr,
offset,
phantom: PhantomData,
}
}
#[inline]
pub fn offset(&self) -> usize {
self.offset
}
#[inline]
pub(super) fn as_ptr(&mut self) -> *mut T {
self.ptr.as_ptr()
}
}
impl<'a, T: ?Sized> InRef<'a, T> {
#[inline]
pub(super) fn cast<U>(self) -> InRef<'a, U> {
InRef::new(self.ptr.cast(), self.offset)
}
#[inline]
pub(super) fn cast_slice<U>(self, len: usize) -> InRef<'a, [U]> {
InRef::new(
NonNull::slice_from_raw_parts(self.ptr.cast(), len),
self.offset,
)
}
}
impl<'a, T> InRef<'a, [T]> {
#[inline]
pub fn len(&self) -> usize {
self.ptr.len()
}
}
impl<T: Copy> InRef<'_, T> {
#[inline]
pub fn copy_from(&mut self, _: &impl Committer, src: impl Borrow<T>) {
unsafe { self.as_ptr().copy_from_nonoverlapping(src.borrow(), 1) }
}
}
impl<T: ?Sized + Copy> InRef<'_, [T]> {
#[inline]
pub unsafe fn copy_from_iter_unchecked(
&mut self,
_: &impl Committer,
dest: impl IntoIterator<Item = impl AsRef<[T]>>,
) {
dest.into_iter()
.fold(self.as_ptr().cast::<T>(), |ptr, src| {
let src = src.as_ref();
let len = src.len();
ptr.copy_from_nonoverlapping(src.as_ptr(), len);
ptr.add(len)
});
}
#[inline]
pub unsafe fn copy_from_unchecked(&mut self, com: &impl Committer, src: impl AsRef<[T]>) {
self.copy_from_iter_unchecked(com, once(src))
}
}
pub struct Input<'a, T: ?Sized, U> {
data_ref: InRef<'a, T>,
val: U,
}
impl<'a, T: ?Sized, U> Input<'a, T, U> {
#[inline]
pub unsafe fn new_unchecked(data_ref: InRef<'a, T>, val: U) -> Self {
Self { data_ref, val }
}
}
impl<T: ?Sized, U> Input<'_, T, U> {
#[inline]
pub fn offset(&self) -> usize {
self.data_ref.offset()
}
}
impl<T, U> Input<'_, [T], U> {
#[inline]
pub fn len(&self) -> usize {
self.data_ref.len()
}
}
impl<'a, T> InRef<'a, T> {
#[inline]
pub fn stage<U: Borrow<T>>(self, val: U) -> Input<'a, T, U> {
Input {
data_ref: self,
val,
}
}
}
impl<'a, T> InRef<'a, [T]> {
#[inline]
pub fn stage_slice<U: AsRef<[T]>>(self, val: U) -> Input<'a, [T], U> {
Input {
data_ref: self,
val,
}
}
}
impl<'a, T, U: Borrow<T>> Input<'a, T, U> {
#[inline]
pub fn stage(alloc: &mut impl Allocator, val: U) -> Result<Self> {
alloc
.allocate_input()
.map(move |data_ref| data_ref.stage(val))
}
}
impl<'a, T, U: AsRef<[T]>> Input<'a, [T], U> {
#[inline]
pub fn stage_slice(alloc: &mut impl Allocator, val: U) -> Result<Self> {
alloc
.allocate_input_slice(val.as_ref().len())
.map(move |data_ref| data_ref.stage_slice(val))
}
}
impl<'a, T> Input<'a, [T], &'a [T]> {
#[inline]
pub fn stage_slice_max(alloc: &mut impl Allocator, val: &'a [T]) -> Result<(Self, &'a [T])> {
let (head, tail) = val.split_at(val.len().min(alloc.free::<T>()));
Self::stage_slice(alloc, head).map(|input| (input, tail))
}
}
impl<'a, T: Copy, U: Borrow<T>> Commit for Input<'a, T, U> {
type Item = ();
#[inline]
fn commit(mut self, com: &impl Committer) {
self.data_ref.copy_from(com, self.val)
}
}
impl<'a, T: ?Sized + Copy, U: AsRef<[T]>> Commit for Input<'a, [T], U> {
type Item = ();
#[inline]
fn commit(mut self, com: &impl Committer) {
unsafe { self.data_ref.copy_from_unchecked(com, self.val) }
}
}