1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// Returns a function that discards its argument and always returns `r`.
///
/// ```
/// use toad_common::*;
///
/// fn try_get_string() -> Result<String, std::io::Error> {
///   # Ok("".into())
/// }
///
/// fn do_stuff() -> Result<String, std::io::Error> {
///   try_get_string().map(const_("it worked!".to_string())) // equivalent to:
///                   .map(|_| "it worked!".to_string())
/// }
/// ```
pub fn const_<T, R>(r: R) -> impl FnOnce(T) -> R {
  |_| r
}

/// A function that discards its argument and always returns unit `()`
///
/// ```
/// use toad_common::*;
///
/// fn try_get_string() -> Result<String, std::io::Error> {
///   # Ok("".into())
/// }
///
/// fn do_stuff() -> Result<(), std::io::Error> {
///   try_get_string().map(ignore) // equivalent to:
///                   .map(|_| ())
/// }
/// ```
pub fn ignore<T>(_: T) {
  ()
}