over

Function over 

Source
pub fn over<Root, Value>(
    keypath: KeyPaths<Root, Value>,
    update: impl Fn(Value) -> Value + 'static,
) -> impl Fn(Root) -> Root
where Root: Clone + 'static, Value: Clone + 'static,
Expand description

Produces an immutable setter function for a given key path and update function. Equivalent to Swift’s over<Root, Value>(_ keyPath: WritableKeyPath<Root, Value>, _ update: @escaping (Value) -> Value) -> (Root) -> Root

§Examples

use rust_overture::keypaths::over;
use key_paths_core::KeyPaths;

#[derive(Clone)]
struct Person {
    name: String,
    age: u32,
}

let age_keypath = KeyPaths::writable(|person: &mut Person| &mut person.age);
let double_age = over(age_keypath, |age| age * 2);
let person = Person { name: "Alice".to_string(), age: 30 };
let updated = double_age(person);
assert_eq!(updated.age, 60);