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
use std::{iter::FilterMap, slice::IterMut};

#[doc(hidden)]
#[derive(Debug)]
pub struct Iter<'a, T: 'a>(FilterMap<IterMut<'a, Option<T>>, fn(&'a mut Option<T>) -> Option<T>>);

impl<'a, T> Iterator for Iter<'a, T> {
    type Item = T;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

#[doc(hidden)]
pub fn mut_options_slice_to_iterator<'a, T>(slice: &'a mut [Option<T>]) -> Iter<'a, T> {
    Iter(
        slice
            .iter_mut()
            .filter_map(extract_owned_option as fn(&'a mut Option<T>) -> Option<T>),
    )
}

fn extract_owned_option<T>(mut_option_ref: &mut Option<T>) -> Option<T> {
    mut_option_ref.take()
}

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

    #[test]
    fn create_an_iterator_that_collects() {
        let mut buf = vec![Some(String::from("blah")); 3];

        let slice = &mut buf[..];

        let iterator = mut_options_slice_to_iterator(slice);

        let collection: Vec<String> = iterator.collect();

        assert_eq!(collection, vec![String::from("blah"); 3]);
    }

    #[test]
    fn more_than_32_items() {
        let mut buf = vec![Some(String::from("blah")); 36];

        let slice = &mut buf[..];

        let iterator = mut_options_slice_to_iterator(slice);

        let collection: Vec<String> = iterator.collect();

        assert_eq!(collection, vec![String::from("blah"); 36]);
    }
}