#[fp_macros::document_module]
mod inner {
use {
crate::{
Apply,
brands::optics::*,
classes::{
CloneableFn,
profunctor::{
Choice,
Profunctor,
Strong,
},
},
impl_kind,
kinds::*,
},
fp_macros::*,
};
#[document_type_parameters(
"The lifetime of the functions.",
"The cloneable function brand.",
"The type of the value produced by the preview function.",
"The type of the value consumed by the setter.",
"The source type of the structure.",
"The target type of the structure."
)]
pub struct Stall<'a, FunctionBrand: CloneableFn, A: 'a, B: 'a, S: 'a, T: 'a> {
pub get: <FunctionBrand as CloneableFn>::Of<'a, S, Result<A, T>>,
pub set: <FunctionBrand as CloneableFn>::Of<'a, (S, B), T>,
}
#[document_type_parameters(
"The lifetime of the functions.",
"The cloneable function brand.",
"The type of the value produced by the preview function.",
"The type of the value consumed by the setter.",
"The source type of the structure.",
"The target type of the structure."
)]
impl<'a, FunctionBrand: CloneableFn, A: 'a, B: 'a, S: 'a, T: 'a>
Stall<'a, FunctionBrand, A, B, S, T>
{
#[document_signature]
#[document_parameters("The preview function.", "The setter function.")]
#[document_returns("A new instance of the type.")]
#[document_examples]
pub fn new(
get: <FunctionBrand as CloneableFn>::Of<'a, S, Result<A, T>>,
set: <FunctionBrand as CloneableFn>::Of<'a, (S, B), T>,
) -> Self {
Stall {
get,
set,
}
}
}
impl_kind! {
impl<FunctionBrand: CloneableFn + 'static, A: 'static, B: 'static> for StallBrand<FunctionBrand, A, B> {
#[document_default]
type Of<'a, S: 'a, T: 'a>: 'a = Stall<'a, FunctionBrand, A, B, S, T>;
}
}
#[document_type_parameters(
"The cloneable function brand.",
"The type of the value produced by the preview function.",
"The type of the value consumed by the setter."
)]
impl<FunctionBrand: CloneableFn + 'static, A: 'static, B: 'static> Profunctor
for StallBrand<FunctionBrand, A, B>
{
#[document_signature]
#[document_type_parameters(
"The lifetime of the functions.",
"The source type of the new structure.",
"The target type of the new structure.",
"The source type of the original structure.",
"The target type of the original structure."
)]
#[document_parameters(
"The function to apply to the input.",
"The function to apply to the output.",
"The stall instance to transform."
)]
#[document_returns("A transformed `Stall` instance.")]
#[document_examples]
fn dimap<'a, S: 'a, T: 'a, U: 'a, V: 'a>(
st: impl Fn(S) -> T + 'a,
uv: impl Fn(U) -> V + 'a,
puv: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, T, U>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, V>) {
let get = puv.get;
let set = puv.set;
let st = <FunctionBrand as CloneableFn>::new(st);
let uv = <FunctionBrand as CloneableFn>::new(uv);
let st_2 = st.clone();
let uv_2 = uv.clone();
Stall::new(
<FunctionBrand as CloneableFn>::new(move |s: S| {
(*get)((*st)(s)).map_err(|u| (*uv)(u))
}),
<FunctionBrand as CloneableFn>::new(move |(s, b): (S, B)| {
(*uv_2)((*set)(((*st_2)(s), b)))
}),
)
}
}
#[document_type_parameters(
"The cloneable function brand.",
"The type of the value produced by the preview function.",
"The type of the value consumed by the setter."
)]
impl<FunctionBrand: CloneableFn + 'static, A: 'static, B: 'static> Strong
for StallBrand<FunctionBrand, A, B>
{
#[document_signature]
#[document_type_parameters(
"The lifetime of the functions.",
"The source type of the structure.",
"The target type of the structure.",
"The type of the other component."
)]
#[document_parameters("The stall instance to transform.")]
#[document_returns("A transformed `Stall` instance that operates on tuples.")]
#[document_examples]
fn first<'a, S: 'a, T: 'a, C: 'a>(
pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, (S, C), (T, C)>) {
let get = pab.get;
let set = pab.set;
Stall::new(
<FunctionBrand as CloneableFn>::new(move |(s, c): (S, C)| {
(*get)(s).map_err(|t| (t, c))
}),
<FunctionBrand as CloneableFn>::new(move |((s, c), b): ((S, C), B)| {
((*set)((s, b)), c)
}),
)
}
}
#[document_type_parameters(
"The cloneable function brand.",
"The type of the value produced by the preview function.",
"The type of the value consumed by the setter."
)]
impl<FunctionBrand: CloneableFn + 'static, A: 'static, B: 'static> Choice
for StallBrand<FunctionBrand, A, B>
{
#[document_signature]
#[document_type_parameters(
"The lifetime of the functions.",
"The source type of the structure.",
"The target type of the structure.",
"The type of the other component."
)]
#[document_parameters("The stall instance to transform.")]
#[document_returns(
"A transformed `Stall` instance that operates on the left component of a `Result`."
)]
#[document_examples]
fn left<'a, S: 'a, T: 'a, C: 'a>(
pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, Result<C, S>, Result<C, T>>)
{
let get = pab.get;
let set = pab.set;
Stall::new(
<FunctionBrand as CloneableFn>::new(move |r: Result<C, S>| match r {
Err(s) => (*get)(s).map_err(Err),
Ok(c) => Err(Ok(c)),
}),
<FunctionBrand as CloneableFn>::new(move |(r, b): (Result<C, S>, B)| match r {
Err(s) => Err((*set)((s, b))),
Ok(c) => Ok(c),
}),
)
}
#[document_signature]
#[document_type_parameters(
"The lifetime of the functions.",
"The source type of the structure.",
"The target type of the structure.",
"The type of the other component."
)]
#[document_parameters("The stall instance to transform.")]
#[document_returns(
"A transformed `Stall` instance that operates on the right component of a `Result`."
)]
#[document_examples]
fn right<'a, S: 'a, T: 'a, C: 'a>(
pab: Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, S, T>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a, U: 'a>: 'a; )>::Of<'a, Result<S, C>, Result<T, C>>)
{
let get = pab.get;
let set = pab.set;
Stall::new(
<FunctionBrand as CloneableFn>::new(move |r: Result<S, C>| match r {
Ok(s) => (*get)(s).map_err(Ok),
Err(c) => Err(Err(c)),
}),
<FunctionBrand as CloneableFn>::new(move |(r, b): (Result<S, C>, B)| match r {
Ok(s) => Ok((*set)((s, b))),
Err(c) => Err(c),
}),
)
}
}
}
pub use inner::*;