set

Function set 

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

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

§Examples

use rust_overture::keypaths::set;
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 set_age_25 = set(age_keypath, 25);
let person = Person { name: "Alice".to_string(), age: 30 };
let updated = set_age_25(person);
assert_eq!(updated.age, 25);