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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! # `lexe-std`
//!
//! This crate contains "std extensions" which other Lexe crates can use without
//! having to pull in any dependencies.
//!
//! Traits, macros, copies of unstable `std` APIs, a small number of types, are
//! all fair game so long as they do NOT depend on anything outside of [`std`].
// Re-export `std` for use by macros.
pub use std;
/// `[u8; N]` array functions.
/// Exponential backoff.
/// Utilities for use in `const` fns and expressions.
/// `fmt` extensions.
/// Iterator extensions.
/// Path extensions.
/// String utilities.
/// A trait which allows us to apply functions (including tuple enum variants)
/// to non-[`Iterator`]/[`Result`]/[`Option`] values for cleaner iterator-like
/// chains. It exposes an [`apply`] method and is implemented for all `T`.
///
/// For example, instead of this:
///
/// ```ignore
/// # use lexe_common::ln::amount::Amount;
/// let value_sat_u64 = 100_000u64; // Pretend this is from LDK
/// let value_sat_u32 = u32::try_from(value_sat_u64)
/// .expect("Amount shouldn't have overflowed");
/// let maybe_value = Some(Amount::from_sats_u32(value_sat_u32));
/// ```
///
/// We can remove the useless `value_sat_u32` intermediate variable:
///
/// ```ignore
/// # use lexe_common::ln::amount::Amount;
/// # use lexe_common::Apply;
/// let value_sat_u64 = 100_000u64; // Pretend this is from LDK
/// let maybe_value = u32::try_from(value_sat_u64)
/// .expect("Amount shouldn't have overflowed")
/// .apply(Amount::from_sats_u32)
/// .apply(Some);
/// ```
///
/// Without having to add use nested [`Option`]s / [`Result`]s which can be
/// confusing:
///
/// ```ignore
/// # use lexe_common::ln::amount::Amount;
/// let value_sat_u64 = 100_000u64; // Pretend this is from LDK
/// let maybe_value = u32::try_from(value_sat_u64)
/// .map(Amount::from_sats_u32)
/// .map(Some)
/// .expect("Amount shouldn't have overflowed");
/// ```
///
/// Overall, this trait makes it easier to both (1) write iterator chains
/// without unwanted intermediate variables and (2) write them in a way that
/// maximizes clarity and readability, instead of having to reorder our
/// `.transpose()` / `.map()`/ `.expect()` / `.context("...")?` operations
/// to "stay inside" the function chain.
///
/// [`apply`]: Self::apply