Skip to main content

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