1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! An experimental module providing a way to update method parameters in aspects.

/// The trait `Update` allows a value to be updated while guaranteeing the return type is the same.
pub trait Update<T> {
    /// Update a value
    fn update(_: T) -> T;
}

/// The trait `UpdateRef` allows a value to be updated by using its reference.
pub trait UpdateRef<T> {
    /// Update a value by reference
    fn update_ref(_: &mut T);
}

impl<T, U: UpdateRef<T>> Update<T> for U {
    fn update(mut t: T) -> T {
        <U as UpdateRef<_>>::update_ref(&mut t);
        t
    }
}


#[cfg(test)]
mod test {
    use super::{Update, UpdateRef};

    struct Doubler;

    impl<'a> Update<&'a mut u64> for Doubler {
        fn update(t: &'a mut u64) -> &'a mut u64 {
            Doubler::update_ref(t);
            t
        }
    }
    // Can also be implemented on reference types
    impl UpdateRef<u64> for Doubler {
        fn update_ref(t: &mut u64) {
            (*t) *= 2;
        }
    }

    fn intercepted(foo: u64) -> u64 {
        foo + 2
    }

    fn intercepted_ref(foo: &mut u64) {
        (*foo) += 2
    }

    #[test]
    fn test() {
        fn _intercepted(foo: u64) -> u64 {
            // Use shadowing to update a value and guarantee its type
            let foo = <Doubler as Update<_>>::update(foo);
            intercepted(foo)
        }

        fn _intercepted_ref(foo: &mut u64) {
            // Use shadowing to update a value and guarantee its type
            let foo = <Doubler as Update<_>>::update(foo);
            intercepted_ref(foo)
        }

        let mut v = 42;
        let r = _intercepted(v);
        assert_eq!(r, 86);

        _intercepted_ref(&mut v);
        assert_eq!(v, 86);
    }
}