pub fn resolve_option_general<T, ISF, DF>(
input: Option<Option<&T>>,
is_sentinel: ISF,
def: DF,
) -> Option<T>Expand description
Resolves an Option<Option<&T>> (in a builder) into an Option<T>, more generally
Like resolve_option, but:
-
Doesn’t rely on
TbeingDefault + PartialEqto determine whether it’s the sentinel value; instead, takesis_sentinel. -
Takes
Option<Option<&T>>which is more general, but less like the usual call sites.
§Behavior
-
If the input is
None, this indicates that the user did not specify a value, and we therefore usedefto obtain the default value. -
If the input is
Some(None), orSome(Some(v))whereis_sentinel(v)returns true, the user has explicitly specified that this config item should be null/none/nothing, so we returnNone. -
Otherwise the user provided an actual value, and we return
Someof it.
See https://gitlab.torproject.org/tpo/core/arti/-/issues/488
§⚠ Stability Warning ⚠
We may significantly change this so that it is an method in an extension trait.
§Example
use tor_config::resolve_option_general;
// Use 0 as a sentinel meaning "explicitly clear" in this example
let is_sentinel = |v: &i32| *v == 0;
// No value provided: use default
assert_eq!(
resolve_option_general(None, is_sentinel, || Some(10)),
Some(10),
);
// Explicitly None
assert_eq!(
resolve_option_general(Some(None), is_sentinel, || Some(10)),
None,
);
// Sentinel value (0) -> return None
assert_eq!(
resolve_option_general(Some(Some(&0)), is_sentinel, || Some(10)),
None,
);
// Set to actual value -> return that value
assert_eq!(
resolve_option_general(Some(Some(&5)), is_sentinel, || Some(10)),
Some(5),
);