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
//! Mutate a slice in place with a map function.
//!
//! Note that the map result type must be the same as the input type.
//!
//! ## Example
//!
//! ```rust
//! use slice_mip::MapInPlace;
//!
//! let mut buf = [1, 2, 3, 4];
//! buf.map_in_place(|x| x * 2);
//!
//! assert_eq!(buf, [2, 4, 6, 8]);
//! ```

/// A sequence that can be mutated in place.
pub trait MapInPlace<T> {
    /// Apply the given map function to each item in the current sequence and replace each
    /// item with its associated map result.
    fn map_in_place<F>(&mut self, f: F) where F: FnMut(&T) -> T;
}

impl<T> MapInPlace<T> for [T] {
    fn map_in_place<F>(&mut self, mut f: F) where F: FnMut(&T) -> T {
        for item in self.iter_mut() {
            *item = f(item);
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_map_in_place() {
        let mut buf = [0, 1, 2, 3];
        buf.map_in_place(|x| x + 1);

        assert_eq!(buf, [1, 2, 3, 4]);
    }
}