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
use std::mem;
/// Map the value of a mutable reference.
/// Useful if you want to apply a `FnOnce(T) -> T` to a `&mut T`.
/// This function is unsafe because panicking in `f`
/// would leave the pointee uninitialized.
///
/// # Example
/// ```
/// use monster::incubation::map_ref_mut;
///
/// let foo = &mut 123;
///
/// unsafe { map_ref_mut(foo, |n| n + 654) }
///
/// assert_eq!(*foo, 777);
/// ```
pub unsafe fn map_ref_mut<T, F>(thing: &mut T, f: F) where
F: FnOnce(T) -> T
{
let dummy: T = mem::uninitialized();
let owned_thing = mem::replace(thing, dummy);
let new_thing = f(owned_thing);
let dummy = mem::replace(thing, new_thing);
mem::forget(dummy);
}
pub trait MapRefMutExt: Sized {
/// Map the value of a mutable reference.
/// Useful if you want to apply a `FnOnce(T) -> T` to a `&mut T`.
/// This function is unsafe because panicking in `f`
/// would leave the pointee uninitialized.
///
/// # Example
/// ```
/// use monster::incubation::MapRefMutExt;
///
/// let mut foo = &mut 123;
///
/// unsafe { foo.map_ref_mut(|n| n + 654) }
///
/// assert_eq!(*foo, 777);
/// ```
unsafe fn map_ref_mut<F>(&mut self, f: F) where
F: FnOnce(Self) -> Self
{
map_ref_mut(self, f)
}
}
impl <T> MapRefMutExt for T {}