fp_library/classes/pointed.rs
1//! Contexts that can be initialized with a value via the [`pure`] operation.
2//!
3//! ### Examples
4//!
5//! ```
6//! use fp_library::{functions::*, brands::*};
7//!
8//! let x = pure::<OptionBrand, _>(5);
9//! assert_eq!(x, Some(5));
10//! ```
11
12use crate::{Apply, kinds::*};
13use fp_macros::doc_params;
14use fp_macros::doc_type_params;
15use fp_macros::hm_signature;
16
17/// A type class for contexts that can be initialized with a value.
18pub trait Pointed: Kind_cdc7cd43dac7585f {
19 /// The value wrapped in the context.
20 ///
21 /// This method wraps a value in a context.
22 ///
23 /// ### Type Signature
24 ///
25 #[hm_signature(Pointed)]
26 ///
27 /// ### Type Parameters
28 ///
29 #[doc_type_params("The lifetime of the value.", "The type of the value to wrap.")]
30 ///
31 /// ### Parameters
32 ///
33 #[doc_params("The value to wrap.")]
34 ///
35 /// ### Returns
36 ///
37 /// A new context containing the value.
38 ///
39 /// ### Examples
40 ///
41 /// ```
42 /// use fp_library::{functions::*, brands::*};
43 ///
44 /// let x = pure::<OptionBrand, _>(5);
45 /// assert_eq!(x, Some(5));
46 /// ```
47 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>);
48}
49
50/// The value wrapped in the context.
51///
52/// Free function version that dispatches to [the type class' associated function][`Pointed::pure`].
53///
54/// ### Type Signature
55///
56#[hm_signature(Pointed)]
57///
58/// ### Type Parameters
59///
60#[doc_type_params(
61 "The lifetime of the value.",
62 "The brand of the context.",
63 "The type of the value to wrap."
64)]
65///
66/// ### Parameters
67///
68#[doc_params("The value to wrap.")]
69///
70/// ### Returns
71///
72/// A new context containing the value.
73///
74/// ### Examples
75///
76/// ```
77/// use fp_library::{functions::*, brands::*};
78///
79/// let x = pure::<OptionBrand, _>(5);
80/// assert_eq!(x, Some(5));
81/// ```
82pub fn pure<'a, Brand: Pointed, A: 'a>(
83 a: A
84) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
85 Brand::pure(a)
86}