toad_common/
fns.rs

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