conflate/
option.rs

1// SPDX-FileCopyrightText: 2020 Robin Krahl <robin.krahl@ireas.org>
2// SPDX-License-Identifier: Apache-2.0 or MIT
3
4//! Merge strategies for `Option`
5
6/// Overwrite the left value with the right value if the right value is `Some`.
7pub fn overwrite_with_some<T>(left: &mut Option<T>, right: Option<T>) {
8    if right.is_some() {
9        *left = right;
10    }
11}
12
13/// Overwrite `left` with `right` only if `left` is `None`.
14pub fn overwrite_none<T>(left: &mut Option<T>, right: Option<T>) {
15    if left.is_none() {
16        *left = right;
17    }
18}
19
20/// If both `left` and `right` are `Some`, recursively merge the two.
21/// Otherwise, fall back to `overwrite_none`.
22pub fn recurse<T: crate::Merge>(left: &mut Option<T>, right: Option<T>) {
23    if let Some(new) = right {
24        if let Some(original) = left {
25            original.merge(new);
26        } else {
27            *left = Some(new);
28        }
29    }
30}