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<FnBrand: ClonableFn, A>(PhantomData<(FnBrand, 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, FnBrand: ClonableFn>(PhantomData<(OnceBrand, FnBrand)>);
39
40/// [Brand][crate::brands] for [`std::cell::OnceCell`].
41#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
42pub struct OnceCellBrand;
43
44/// [Brand][crate::brands] for [`std::sync::OnceLock`].
45#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
46pub struct OnceLockBrand;
47
48/// [Brand][crate::brands] for [`Option`].
49#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
50pub struct OptionBrand;
51
52/// [Brand][crate::brands] for [`crate::types::Pair`].
53#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
54pub struct PairBrand;
55
56/// [Brand][crate::brands] for the partially-applied form of [`crate::types::Pair`] with [the first value][crate::types::Pair] filled in.
57#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
58pub struct PairWithFirstBrand<First>(First);
59
60/// [Brand][crate::brands] for the partially-applied form of [`crate::types::Pair`] with [the second value][crate::types::Pair] filled in.
61#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
62pub struct PairWithSecondBrand<Second>(Second);
63
64/// [Brand][crate::brands] for [reference-counted][std::rc::Rc] [closures][Fn]
65/// (`Rc<dyn Fn(A) -> B>`).
66///
67/// This struct implements [`ClonableFn`] to provide a way to construct and
68/// type-check [`std::rc::Rc`]-wrapped closures in a generic context. The lifetime `'a`
69/// ensures the closure doesn't outlive referenced data, while `A` and `B`
70/// represent input and output types.
71#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
72pub struct RcFnBrand;
73
74/// [Brand][crate::brands] for [`Result`].
75#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
76pub struct ResultBrand;
77
78/// [Brand][crate::brands] for the partially-applied form of [`Result`] with the [`Err`] constructor filled in.
79#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
80pub struct ResultWithErrBrand<E>(E);
81
82/// [Brand][crate::brands] for the partially-applied form of [`Result`] with the [`Ok`] constructor filled in.
83#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
84pub struct ResultWithOkBrand<T>(T);
85
86/// [Brand][crate::brands] for [`Vec`].
87#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
88pub struct VecBrand;