rustic-rs / conflate

Merge multiple values into one
Apache License 2.0
1 stars 0 forks source link

Add `option::overwrite_with_some` #31

Open simonsan opened 1 hour ago

simonsan commented 1 hour ago

In ::option we have already overwrite_none (Overwrite left with right only if left is None.) and recurse (If both left and right are Some and implement Merge, recursively merge the two.), but when merging values that don't implement Merge (e.g. SocketAddress) from a CLI into a default config, I feel I'm missing overwrite_with_some. Because in the default configuration, there might be already optional values set as Some(T), but the CLI values should still have higher precedence.

/// Overwrite the left value with the right value if the right value is `Some`.
fn overwrite_with_some<T>(left: &mut Option<T>, right: Option<T>) {
    if right.is_some() {
        *left = right;
    }
}
simonsan commented 1 hour ago

I think that also addresses the removal of the blanket implementation for Option<T> from https://github.com/rustic-rs/conflate/blob/c70b6a50a2f43ad9c1f59b056908559f21dc9122/src/lib.rs#L139 :

impl<T> Merge for Option<T> {
    fn merge(&mut self, mut other: Self) {
        if !self.is_some() {
            *self = other.take();
        }
    }
}