fp_library/brands.rs
1//! Higher-kinded representations of [types][crate::types].
2//!
3//! Brands represent higher-kinded (unapplied/partially-applied) forms of
4//! [types][crate::types], as opposed to concrete types, which are
5//! fully-applied.
6//!
7//! For example, [`VecBrand`] represents the higher-kinded type `Vec`, whereas
8//! `Vec A`/`Vec<A>` is the concrete type where `Vec` has been applied to some
9//! generic type `A`.
10
11use crate::classes::{category::Category, clonable_fn::ClonableFn, once::Once};
12use std::marker::PhantomData;
13
14/// [Brand][crate::brands] for [atomically reference-counted][std::sync::Arc]
15/// [closures][Fn] (`Arc<dyn Fn(A) -> B>`).
16///
17/// This struct implements [`ClonableFn`] to provide a way to construct and
18/// type-check [`std::sync::Arc`]-wrapped closures in a generic context. The lifetime `'a`
19/// ensures the closure doesn't outlive referenced data, while `A` and `B`
20/// represent input and output types.
21#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct ArcFnBrand;
23
24/// [Brand][crate::brands] for [`crate::types::Endofunction`].
25#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
26pub struct EndofunctionBrand<ClonableFnBrand: ClonableFn, A>(PhantomData<(ClonableFnBrand, A)>);
27
28/// [Brand][crate::brands] for [`crate::types::Endomorphism`].
29#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
30pub struct EndomorphismBrand<CategoryBrand: Category, A>(PhantomData<(CategoryBrand, A)>);
31
32/// [Brand][crate::brands] for [`crate::types::Identity`].
33#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
34pub struct IdentityBrand;
35
36/// [Brand][crate::brands] for [`crate::types::Lazy`].
37#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
38pub struct LazyBrand<OnceBrand: Once, ClonableFnBrand: ClonableFn>(
39 PhantomData<(OnceBrand, ClonableFnBrand)>,
40);
41
42/// [Brand][crate::brands] for [`std::cell::OnceCell`].
43#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
44pub struct OnceCellBrand;
45
46/// [Brand][crate::brands] for [`std::sync::OnceLock`].
47#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
48pub struct OnceLockBrand;
49
50/// [Brand][crate::brands] for [`Option`].
51#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
52pub struct OptionBrand;
53
54/// [Brand][crate::brands] for [`crate::types::Pair`].
55#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
56pub struct PairBrand;
57
58/// [Brand][crate::brands] for the partially-applied form of [`crate::types::Pair`] with [the first value][crate::types::Pair] filled in.
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
60pub struct PairWithFirstBrand<First>(First);
61
62/// [Brand][crate::brands] for the partially-applied form of [`crate::types::Pair`] with [the second value][crate::types::Pair] filled in.
63#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub struct PairWithSecondBrand<Second>(Second);
65
66/// [Brand][crate::brands] for [reference-counted][std::rc::Rc] [closures][Fn]
67/// (`Rc<dyn Fn(A) -> B>`).
68///
69/// This struct implements [`ClonableFn`] to provide a way to construct and
70/// type-check [`std::rc::Rc`]-wrapped closures in a generic context. The lifetime `'a`
71/// ensures the closure doesn't outlive referenced data, while `A` and `B`
72/// represent input and output types.
73#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
74pub struct RcFnBrand;
75
76/// [Brand][crate::brands] for [`Result`].
77#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
78pub struct ResultBrand;
79
80/// [Brand][crate::brands] for the partially-applied form of [`Result`] with the [`Err`] constructor filled in.
81#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
82pub struct ResultWithErrBrand<E>(E);
83
84/// [Brand][crate::brands] for the partially-applied form of [`Result`] with the [`Ok`] constructor filled in.
85#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
86pub struct ResultWithOkBrand<T>(T);
87
88/// [Brand][crate::brands] for [`Vec`].
89#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
90pub struct VecBrand;