Skip to main content

yui_core/ext/
clone_and.rs

1//! [`CloneAnd`]: clone a value, mutate the clone, and return it.
2
3/// Adds `clone_and(f)`: clone `self`, apply the mutation `f`, and return
4/// the modified clone — for building variants without mutating the original.
5pub trait CloneAnd where Self: Clone {
6    fn clone_and<F>(&self, f: F) -> Self
7    where F: FnOnce(&mut Self) {
8        let mut cloned = self.clone();
9        f(&mut cloned);
10        cloned
11    }
12}
13
14impl<T> CloneAnd for T where T: Clone {}