use core::{fmt, marker::Destruct, ops::Try};
use array_trait::length::LengthValue;
use crate::{Bulk, DoubleEndedBulk, SplitBulk, util::Mutator};
#[must_use = "bulks are lazy and do nothing unless consumed"]
#[derive(Clone)]
pub struct Mutate<I, F>
where
I: Bulk,
F: FnMut(&mut I::Item)
{
bulk: I,
f: F
}
impl<I, F> Mutate<I, F>
where
I: Bulk,
F: FnMut(&mut I::Item)
{
pub(crate) const fn new(bulk: I, f: F) -> Self
{
Self { bulk, f }
}
}
impl<I, F> fmt::Debug for Mutate<I, F>
where
I: Bulk + fmt::Debug,
F: FnMut(&mut I::Item)
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
let Self { bulk, f: _ } = self;
f.debug_struct("Inspect").field("bulk", bulk).finish()
}
}
const impl<I, F> IntoIterator for Mutate<I, F>
where
I: Bulk + [const] IntoIterator<IntoIter: [const] Iterator>,
F: FnMut(&mut I::Item)
{
type IntoIter = core::iter::Map<I::IntoIter, Mutator<F>>;
type Item = I::Item;
fn into_iter(self) -> Self::IntoIter
{
let Self { bulk, f } = self;
bulk.into_iter().map(Mutator(f))
}
}
const impl<I, F> Bulk for Mutate<I, F>
where
I: [const] Bulk,
F: [const] FnMut(&mut I::Item) + [const] Destruct
{
type MaxLength = I::MaxLength;
type MinLength = I::MinLength;
fn len(&self) -> usize
{
self.bulk.len()
}
fn is_empty(&self) -> bool
{
self.bulk.is_empty()
}
fn first(self) -> Option<Self::Item>
where
Self::Item: [const] Destruct,
Self: Sized
{
let Self { bulk, f: mut inspect } = self;
bulk.first().map(Mutator(&mut inspect))
}
fn for_each<FF>(self, f: FF)
where
Self: Sized,
FF: [const] FnMut(Self::Item) + [const] Destruct
{
let Self { bulk, f: mutate } = self;
bulk.for_each(Closure { mutate, f });
}
fn try_for_each<FF, R>(self, f: FF) -> R
where
Self: Sized,
Self::Item: [const] Destruct,
FF: [const] FnMut(Self::Item) -> R + [const] Destruct,
R: [const] Try<Output = ()>
{
let Self { bulk, f: mutate } = self;
bulk.try_for_each(Closure { mutate, f })
}
}
const impl<I, F> DoubleEndedBulk for Mutate<I, F>
where
I: [const] DoubleEndedBulk,
F: [const] FnMut(&mut I::Item) + [const] Destruct
{
fn rev_for_each<FF>(self, f: FF)
where
Self: Sized,
FF: [const] FnMut(Self::Item) + [const] Destruct
{
let Self { bulk, f: mutate } = self;
bulk.rev_for_each(Closure { mutate, f });
}
fn try_rev_for_each<FF, R>(self, f: FF) -> R
where
Self: Sized,
Self::Item: [const] Destruct,
FF: [const] FnMut(Self::Item) -> R + [const] Destruct,
R: [const] Try<Output = ()>
{
let Self { bulk, f: mutate } = self;
bulk.try_rev_for_each(Closure { mutate, f })
}
}
const impl<I, F, L> SplitBulk<L> for Mutate<I, F>
where
I: [const] SplitBulk<L, Left: [const] Bulk, Right: [const] Bulk>,
F: [const] FnMut(&mut I::Item) + [const] Clone + [const] Destruct,
L: LengthValue
{
type Left = Mutate<I::Left, F>;
type Right = Mutate<I::Right, F>;
fn split_at(Self { bulk, f }: Self, n: L) -> (Self::Left, Self::Right)
where
Self: Sized
{
let (left, right) = bulk.split_at(n);
(left.mutate(f.clone()), right.mutate(f))
}
}
struct Closure<F, FF>
{
mutate: F,
f: FF
}
const impl<F, FF, T, R> FnOnce<(T,)> for Closure<F, FF>
where
F: [const] FnOnce(&mut T),
FF: [const] FnOnce(T) -> R
{
type Output = R;
extern "rust-call" fn call_once(self, (mut x,): (T,)) -> Self::Output
{
(self.mutate)(&mut x);
(self.f)(x)
}
}
const impl<F, FF, T, R> FnMut<(T,)> for Closure<F, FF>
where
F: [const] FnMut(&mut T),
FF: [const] FnMut(T) -> R
{
extern "rust-call" fn call_mut(&mut self, (mut x,): (T,)) -> Self::Output
{
(self.mutate)(&mut x);
(self.f)(x)
}
}