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