pub fn mutating(mut v: T, f: F) -> T where F: Fn(&mut T), { f(&mut v); v } pub trait MutatingExt { fn mutating(self, f: F) -> Self where F: Fn(&mut Self); fn mutating_mut(&mut self, f: F) -> &mut Self where F: Fn(&mut Self); } impl MutatingExt for T { fn mutating(self, f: F) -> Self where F: Fn(&mut Self), { mutating(self, f) } fn mutating_mut(&mut self, f: F) -> &mut Self where F: Fn(&mut Self), { f(self); self } } pub fn sideeffect(v: T, f: F) -> T where F: Fn(&T), { f(&v); v } pub trait SideffectExt { fn sideeffect(self, f: F) -> Self where F: Fn(&Self); fn sideeffect_ref(&self, f: F) -> &Self where F: Fn(&Self); fn sideeffect_mut(&mut self, f: F) -> &mut Self where F: Fn(&Self); } impl SideffectExt for T { fn sideeffect(self, f: F) -> Self where F: Fn(&Self), { sideeffect(self, f) } fn sideeffect_ref(&self, f: F) -> &Self where F: Fn(&Self), { f(self); self } fn sideeffect_mut(&mut self, f: F) -> &mut Self where F: Fn(&Self), { f(self); self } } pub fn run R>(f: F) -> R { f() } pub trait ApplyExt: Sized { fn apply(self, f: F) -> R where F: FnOnce(Self) -> R; } impl ApplyExt for T { fn apply(self, f: F) -> R where F: FnOnce(Self) -> R, { f(self) } }