bad-optics/src/fns.rs

186 lines
4.3 KiB
Rust
Raw Normal View History

2021-11-05 15:55:49 +00:00
use crate::{
lenses::{Lens, LensOver, LensView},
2021-11-11 14:47:55 +00:00
prisms::{Prism, PrismPreview},
traversals::{Traversal, TraversalOver, TraversalTraverse},
2021-11-05 15:55:49 +00:00
};
2021-11-05 14:22:59 +00:00
// lens view
impl<L, A> std::ops::FnOnce<(A,)> for Lens<L>
2021-11-05 14:22:59 +00:00
where
L: LensView<A>,
{
type Output = L::Field;
extern "rust-call" fn call_once(self, args: (A,)) -> Self::Output {
2021-11-11 10:39:47 +00:00
L::view(&self.0, args.0)
2021-11-05 14:22:59 +00:00
}
}
impl<L, A> std::ops::FnMut<(A,)> for Lens<L>
2021-11-05 14:22:59 +00:00
where
L: LensView<A>,
{
extern "rust-call" fn call_mut(&mut self, args: (A,)) -> Self::Output {
2021-11-11 10:39:47 +00:00
L::view(&self.0, args.0)
2021-11-05 14:22:59 +00:00
}
}
impl<L, A> std::ops::Fn<(A,)> for Lens<L>
2021-11-05 14:22:59 +00:00
where
L: LensView<A>,
{
extern "rust-call" fn call(&self, args: (A,)) -> Self::Output {
2021-11-11 10:39:47 +00:00
L::view(&self.0, args.0)
2021-11-05 14:22:59 +00:00
}
}
// lens over
impl<L, A, F> std::ops::FnOnce<(A, F)> for Lens<L>
2021-11-05 14:22:59 +00:00
where
L: LensOver<A>,
F: FnOnce(L::Field) -> L::Field,
{
type Output = A;
extern "rust-call" fn call_once(self, args: (A, F)) -> Self::Output {
2021-11-11 10:39:47 +00:00
L::over(&self.0, args.0, args.1)
2021-11-05 14:22:59 +00:00
}
}
impl<L, A, F> std::ops::FnMut<(A, F)> for Lens<L>
2021-11-05 14:22:59 +00:00
where
L: LensOver<A>,
F: FnOnce(L::Field) -> L::Field,
{
extern "rust-call" fn call_mut(&mut self, args: (A, F)) -> Self::Output {
2021-11-11 10:39:47 +00:00
L::over(&self.0, args.0, args.1)
2021-11-05 14:22:59 +00:00
}
}
impl<L, A, F> std::ops::Fn<(A, F)> for Lens<L>
2021-11-05 14:22:59 +00:00
where
L: LensOver<A>,
F: FnOnce(L::Field) -> L::Field,
{
extern "rust-call" fn call(&self, args: (A, F)) -> Self::Output {
2021-11-11 10:39:47 +00:00
L::over(&self.0, args.0, args.1)
2021-11-05 14:22:59 +00:00
}
}
// traversal traverse
impl<L, A> std::ops::FnOnce<(A,)> for Traversal<L>
where
L: TraversalTraverse<A>,
{
type Output = Vec<L::Field>;
extern "rust-call" fn call_once(self, args: (A,)) -> Self::Output {
L::traverse(&self.0, args.0)
}
}
impl<L, A> std::ops::FnMut<(A,)> for Traversal<L>
where
L: TraversalTraverse<A>,
{
extern "rust-call" fn call_mut(&mut self, args: (A,)) -> Self::Output {
L::traverse(&self.0, args.0)
}
}
impl<L, A> std::ops::Fn<(A,)> for Traversal<L>
where
L: TraversalTraverse<A>,
{
extern "rust-call" fn call(&self, args: (A,)) -> Self::Output {
L::traverse(&self.0, args.0)
}
}
// traversal over
impl<L, A, F> std::ops::FnOnce<(A, F)> for Traversal<L>
where
L: TraversalOver<A>,
F: FnMut(L::Field) -> L::Field,
{
type Output = A;
extern "rust-call" fn call_once(self, args: (A, F)) -> Self::Output {
L::over(&self.0, args.0, args.1)
}
}
impl<L, A, F> std::ops::FnMut<(A, F)> for Traversal<L>
where
L: TraversalOver<A>,
F: FnMut(L::Field) -> L::Field,
{
extern "rust-call" fn call_mut(&mut self, args: (A, F)) -> Self::Output {
L::over(&self.0, args.0, args.1)
}
}
impl<L, A, F> std::ops::Fn<(A, F)> for Traversal<L>
where
L: TraversalOver<A>,
F: FnMut(L::Field) -> L::Field,
{
extern "rust-call" fn call(&self, args: (A, F)) -> Self::Output {
L::over(&self.0, args.0, args.1)
}
}
2021-11-11 14:47:55 +00:00
// prism preview
impl<L, A> std::ops::FnOnce<(A,)> for Prism<L>
where
L: PrismPreview<A>,
{
type Output = Option<L::Field>;
extern "rust-call" fn call_once(self, args: (A,)) -> Self::Output {
L::preview(&self.0, args.0)
}
}
impl<L, A> std::ops::FnMut<(A,)> for Prism<L>
where
L: PrismPreview<A>,
{
extern "rust-call" fn call_mut(&mut self, args: (A,)) -> Self::Output {
L::preview(&self.0, args.0)
}
}
impl<L, A> std::ops::Fn<(A,)> for Prism<L>
where
L: PrismPreview<A>,
{
extern "rust-call" fn call(&self, args: (A,)) -> Self::Output {
L::preview(&self.0, args.0)
}
}
// prism over
impl<L, A, F> std::ops::FnOnce<(A, F)> for Prism<L>
where
A: Clone,
L: PrismPreview<A>,
F: FnMut(L::Field) -> L::Field,
{
type Output = A;
extern "rust-call" fn call_once(self, args: (A, F)) -> Self::Output {
L::over(&self.0, args.0, args.1)
}
}
impl<L, A, F> std::ops::FnMut<(A, F)> for Prism<L>
where
A: Clone,
L: PrismPreview<A>,
F: FnMut(L::Field) -> L::Field,
{
extern "rust-call" fn call_mut(&mut self, args: (A, F)) -> Self::Output {
L::over(&self.0, args.0, args.1)
}
}
impl<L, A, F> std::ops::Fn<(A, F)> for Prism<L>
where
A: Clone,
L: PrismPreview<A>,
F: FnMut(L::Field) -> L::Field,
{
extern "rust-call" fn call(&self, args: (A, F)) -> Self::Output {
L::over(&self.0, args.0, args.1)
}
}